package Server.Jsmj.RGCP;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import Server.Agent.GameAgent;
import Server.Agent.GameDB;
import Server.Jsmj.RGCP.QILE.BlockType;
import Server.Jsmj.RGCP.QILE.HuBlock;
import Server.Jsmj.RGCP.QILE.JiangUtil;
import Server.Jsmj.RGCP.QILE.RGCPUtil;
import Server.Model.Base.IRoom;
import Server.Model.Base.IServerUserItem;
import Server.Model.Base.SHMUser;
import Server.Model.Base.UploadUtil;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import activity.ActivityMgr;
import activity.UserActionActivity;
import club.ClubRedis;
import common.routine.PublishDef;
import rank.RankMgr;
import redis.protocal.DbInfoMgr;
class tagUserReport {
	
	public int lUid;
	public int cntZiMo;
	public int cntHu;
	public int cntDianPao;
	public int cntGang;	
	public int lWinScore;
	public int lLoseScore;
	public int lResultScore;

	public String toString() {
		String data = String.format("%d#%d#%d#%d#%d#%d#%d#%d", lUid, cntZiMo, cntHu, cntDianPao,cntGang, lWinScore, lLoseScore,
				lResultScore);

		return data;
	}

}

class SUBCMD_S_REPORT {
	public int cbReason;
	public int cbType;
	public long life_time;
	public tagUserReport userReport[] = new tagUserReport[4];
	{
		for (int i = 0; i < userReport.length; i++) {
			userReport[i] = new tagUserReport();
		}
	}

	public String getJsonData() {

		String sReport0 = userReport[0].toString();
		String sReport1 = userReport[1].toString();
		String sReport2 = userReport[2].toString();
		String sReport3 = userReport[3].toString();
		String mjmsg = String.format("report|%d|%d|%s|%s|%s|%s", cbReason, cbType, sReport0, sReport1, sReport2,
				sReport3);
		JSONObject json = new JSONObject();
		json.put("command", "mjmsg");
		json.put("mjmsg", mjmsg);

		return json.toString();
	}
	
	
	public String getCPJsonData() {

		String sReport0 = userReport[0].toString();
		String sReport1 = userReport[1].toString();
		String sReport2 = userReport[2].toString();
		String sReport3 = userReport[3].toString();
		String mjmsg = String.format("report|%d|%d|%s|%s|%s|%s", cbReason, cbType, sReport0, sReport1, sReport2,
				sReport3);
		JSONObject json = new JSONObject();
		json.put("command", "cpmsg");
		json.put("cpmsg", mjmsg);

		return json.toString();
	}
	
	public String getShuangJiangJsonData() {

		String sReport0 = userReport[0].toString();
		String sReport1 = userReport[1].toString();
		String sReport2 = userReport[2].toString();
		String sReport3 = userReport[3].toString();
		String sjmsg = String.format("report|%d|%d|%s|%s|%s|%s", cbReason, cbType, sReport0, sReport1, sReport2,
				sReport3);
		JSONObject json = new JSONObject();
		json.put("command", "sjmsg");
		json.put("sjmsg", sjmsg);

		return json.toString();
	}

}
	
public class RGCPTable extends  IRoom {

	Replay Replay = new Replay();
	public int maiZhuang = 0;
	public int[] maiZhuangScore = new int[3];
	public int fangzhu;
	public static int PLAYERCOUNT = 3;
	IServerUserItem m_pIUserItem[] = new IServerUserItem[3]; // 用户指针

	M_tempAction m_tempAction[] = new M_tempAction[PLAYERCOUNT];

	{
		for (int i = 0; i < PLAYERCOUNT; i++) {
			m_tempAction[i] = new M_tempAction();
		}
	}

	@Override
	public int GetID() {
		return m_dwTableID;
	};

	@Override
	public String GetType() {
		return "CP";
	};

	@Override
	public int GetCreator() {
		return m_Creator;
	}

	@Override
	public void OnClientClose(int uid) {
		OnUserStandUp(uid,false);
	};

	@Override
	public int CanSit(int _Uid) {
		/*
		 * zws if (m_Creator == _Uid) { return 14; }
		 */
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			if (m_payWay.equalsIgnoreCase("WINPAY") || m_payWay.equalsIgnoreCase("WINPAY")  || m_payWay.equalsIgnoreCase("CLUBPAY"))
			{
				if (user.getGem(0) < m_byCostGem * 4) {
					return 11;
				}
			}else{
				if (m_payWay.equalsIgnoreCase("AAPAY") )
				{
					if (user.getGem(0) < m_byCostGem) {
						return 11;
					}
				}
			}
		}
		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				return i;
			}
		}

		for (int i = 0; i < 4; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				return i;
			}
		}

		return 12;
	};

	@Override
	public int FindSeat(int _Uid) {
		for (int bySeatID = 0; bySeatID < PLAYERCOUNT; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	RGCPLogic m_MajLogic;
	MajPaiQiang m_MajPaiQiang;

	boolean m_bDiTing[] = new boolean[PLAYERCOUNT]; // ��������
	int m_byTing[] = new int[PLAYERCOUNT];
	long m_dwLastDoCmdTime;
	int m_byRight[][] = new int[PLAYERCOUNT][6];
	boolean m_bIsHu[] = new boolean[PLAYERCOUNT]; // �Ƿ����

	int fanJiang1;// ����
	public int[] Jiang;

	boolean m_bCengBao = false;
	// ���һ���Ľ���
	tagUserReport m_kReport[] = new tagUserReport[3];
	{
		for (int i = 0; i < PLAYERCOUNT; i++) {
			m_kReport[i] = new tagUserReport();
		}
	}
	boolean m_bTableStarted;
	boolean m_bGameStarted;
	Long m_lStartTime;
	long needDealTime;
	public int m_dwTableID;
	long m_dismissTimer;
	int m_byAgreeDismiss[] = new int[PLAYERCOUNT];
	public boolean m_jiesuan = false;
	public int maxPeople = 3;
	public int m_byRound;
	public int m_byMaxRound;
	int m_wChuPaiDelay;
	int m_wFaPaiDelay;// �����ӳ�
	long m_wRobotSeconds[] = new long[PLAYERCOUNT]; // �ȴ�ʱ��

	long m_wJieSuanRound;
	public int m_byCostGem;
	public boolean m_bIsDaKun = true;
	public int m_diFen = 0;
	public int m_fdFen = 0;

	public boolean is_xier = true;

	public String m_payWay;
	public String m_strParam;
	public String m_strRoomDesc;

	public String[] m_vecParam;

	public int m_byZhuang = 0;// ��ǰׯλ��
	public int m_byLianZhuang;// ��ǰׯλ��
	public int m_vecWinScore[] = new int[PLAYERCOUNT];
	public int m_vecLoseScore[] = new int[PLAYERCOUNT];
	public int[] m_vecHuType = new int[PLAYERCOUNT];

	int[] huScore = new int[3];// �̶�����
	int[] qishou_huacount = new int[PLAYERCOUNT];
	public String m_vecRoundHuDesc[] = new String[PLAYERCOUNT];
	public int m_vecRoundHushu[] = new int[PLAYERCOUNT];
	public int m_vecRoundScore[] = new int[PLAYERCOUNT];
	public int m_vecResultScore[] = new int[PLAYERCOUNT];
	public int m_vecZiMo[] = new int[PLAYERCOUNT];
	public String[] m_TouXiang = new String[5];
	public int m_fangpao[] = new int[PLAYERCOUNT];

	public int m_Creator;// ����

	// ��������
	public boolean f_shuanglong = false;
	public boolean f_haidilaoyue = false;
	public boolean f_sanlaohuimian = false;

	public int fangzi_end = 0;
	int fangzi[] = new int[PLAYERCOUNT];

	public String room_id = "000";

	// ���ƽ��

	{
		for (int i = 0; i < 3; i++) {
			m_pIUserItem[i] = new IServerUserItem();
		}
	}

	long m_LookOnUserID[] = new long[50];// ����

	RGCPTable() {
		m_byZhuang = 0;
		m_byLianZhuang = 0;
		m_Creator = 0;
		m_wJieSuanRound = 0L;
		m_bTableStarted = false;
		m_strParam = "";
		m_strRoomDesc = "";
		m_byCostGem = 0;
		m_payWay = "FZPAY";
		m_byRound = 0;
		m_byMaxRound = 0;
		m_diFen = 3;
		m_fdFen = 40;
		m_bIsDaKun = true;
		m_bCengBao = false;
	}

	void Init(int cbReason) {
		fangzi_end = 0;
		m_bGameStarted = false;
		m_dwLastDoCmdTime = 0L;
		// m_bTianHu = true;

		m_wChuPaiDelay = 0;
		m_wFaPaiDelay = 0;

		m_bCengBao = false;

		CleanTempAction();

		for (int i = 0; i < PLAYERCOUNT; i++) {
			maiZhuangScore[i] = 0;
			huScore[i] = 0;
			m_bDiTing[i] = false;
			m_bIsHu[i] = false;
			m_byTing[i] = 0;
			m_wRobotSeconds[i] = 0L;
			fangzi[i] = 0;

			m_pIUserItem[i].cbUserStatus = Constants.US_NULL;

			m_vecHuType[i] = 0;
			m_vecRoundHuDesc[i] = "";
			m_vecRoundHushu[i] = 0;
			qishou_huacount[i] = 0;
			m_vecRoundScore[i] = 0;
		}
		for (int i = 0; i < PLAYERCOUNT; i++) {
			m_byRight[i][0] = 0;
			m_byRight[i][1] = 0;
			m_byRight[i][2] = 0;
			m_byRight[i][3] = 0;
			m_byRight[i][4] = 0;
			m_byRight[i][5] = 0;

		}

		if (Constants.INIT_TOTAL == cbReason) {
			m_bTableStarted = false;
			m_dismissTimer = 150L;
			m_wJieSuanRound = 0L;
			m_byAgreeDismiss[0] = 0;
			m_byAgreeDismiss[1] = 0;
			m_byAgreeDismiss[2] = 0;

			Constants.memset(m_LookOnUserID, 0, Constants.sizeof(m_LookOnUserID)); //
			m_MajLogic.Init(Constants.INIT_TOTAL);
			m_byRound = 0;

			for (int i = 0; i < PLAYERCOUNT; i++) {
				m_byAgreeDismiss[i] = 0;
				m_vecWinScore[i] = 0;
				m_vecLoseScore[i] = 0;
				m_vecResultScore[i] = 0;
			}

		}
		if (Constants.INIT_FIRST_ROUND == cbReason) {
			m_MajLogic.Init(Constants.INIT_FIRST_ROUND);
		}
		if (Constants.INIT_NEWROUND == cbReason) {
			m_MajLogic.Init(Constants.INIT_NEWROUND);
		}

	}

	void KickRobots() {
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
	}

	boolean HasRobot() {
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				return true;
			}
		}
		return false;
	}

	void SendAvatar(int _Uid) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		String iter = dthall.getInstance().Handle_OnGetUserAvatar(_Uid);

		SUBCMD_S_Avatar cmd = new SUBCMD_S_Avatar();
		cmd.m_lUid = _Uid;
		if (!StringUtils.isEmpty(iter)) {
			m_TouXiang[wChairID]=iter;
			cmd.szAvatarURL = iter;
		} else {
			cmd.szAvatarURL = PublishDef.getDefaultHeadAvatar();
		}

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	public void SendLocationData(SHMUser user) {
		JSONObject data = new JSONObject();
		data.put("loc", user.getGpsLocInfo());
		data.put("uid", user.getUid());
		data.put("command", "GPS_LOC");
		SendTableData(Constants.INVALID_CARD, data.toString());

		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if(other != null){
						data.put("uid", other.getUid());
						data.put("loc", other.getGpsLocInfo());
						dthall.getInstance().onSend(user.getUid(), data.toString());
					}
				}
			}
		}
	}

	public void SendGpsData(SHMUser user) {
		JSONObject data = new JSONObject();
		List<Double> gpsData = user.getLocateInfo();
		data.put("lng", gpsData.get(0));
		data.put("lat", gpsData.get(1));
		data.put("command", "GPS_DATA");
		data.put("uid", user.getUid());
		SendTableData(Constants.INVALID_CHAIR, data.toString());

		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if (other != null) {
						gpsData = other.getLocateInfo();
						data.put("uid", other.getUid());
						data.put("lng", gpsData.get(0));
						data.put("lat", gpsData.get(1));
						dthall.getInstance().onSend(user.getUid(), data.toString());
					}
				}
			}
		}
	}

	public void OnUserSit(int _Uid) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			SendGpsData(user);
			SendLocationData(user);
		}
		if (user != null) {
			
			for (int i = 0; i < PLAYERCOUNT; i++) {
				if (m_pIUserItem[i].dwUserID == _Uid) {
					m_wRobotSeconds[i] = 0L;
					m_pIUserItem[i].dwUserID = _Uid;

					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = user.getAvatar();
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].cbOnline = 0;
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_vecResultScore[i];
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = m_pIUserItem[i].szIP;

					SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					SendAvatar(m_pIUserItem[i].dwUserID);
					return;
				}
			}
			for (int i = 0; i < PLAYERCOUNT; i++) {
				if (m_pIUserItem[i].dwUserID == 0) {
					m_wRobotSeconds[i] = 0L;
					m_pIUserItem[i].dwUserID = _Uid;
					m_pIUserItem[i].lUserScore = 0;
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = user.getAvatar();
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].cbOnline = 0;
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_vecResultScore[i];
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = m_pIUserItem[i].szIP;

					SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}
					return;
				}
			}
		}
		return;
	}

	int FindSeat(long _Uid) {
		for (int bySeatID = 0; bySeatID < 4; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}
	public int GetMaxPlayerCount(){
		return 3;
	}
	public int GetPlayerCount() {
		int cntPlayer = 0;
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (null != m_pIUserItem[i] && m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	int GetRobotCount() {
		int cntPlayer = 0;
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	public void OnUserStandUp(int _Uid,boolean leaveroom) {
		int wChairID = FindSeat(_Uid);
		if(m_bTableStarted == false && wChairID != 0 && leaveroom == true){
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
		}


		if (m_bTableStarted == false && leaveroom == true) {
			if (wChairID > 0 && wChairID < 5) {
				m_pIUserItem[wChairID].Init();
				QpGameMgr.getInstance().OnRemoveUserRoom(_Uid);
			} else {
			}
		} else {
			if (wChairID < 5) {
				m_pIUserItem[wChairID].cbOnline = Constants.US_OFFLINE;
			}
		}
	}

	public boolean HasParam(String szKey, boolean defaultValue) {
		for (int i = 0; i < m_vecParam.length; i++) {
			String key = m_vecParam[i];
			if (key.equals(szKey)) {
				return true;
			}
		}
		return defaultValue;
	}

	public void SetParam(String szParam) {
		m_strParam = szParam;

		m_vecParam = m_strParam.split(";");
	}

	public void SetDesc(String szDesc) {
		m_strRoomDesc = szDesc;

	}

	boolean SendYuPai() {
		SUBCMD_S_YuPai cmd = new SUBCMD_S_YuPai();
		cmd.byYuPai = m_MajPaiQiang.GetYuPaiCount();
		cmd.byCurrentUser = m_MajLogic.GetCurrentUser();
		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
		return true;
	}

	protected boolean SendGameScene(int _Uid, int bGameStatus) {
		{
			String tmpRoomDesc = m_strRoomDesc;
			SUBCMD_S_Param cmd = new SUBCMD_S_Param();
			cmd.m_dwTableID = m_dwTableID;
			cmd.szParam = m_strParam;
			cmd.szRoomDesc = tmpRoomDesc;
			cmd.m_creater = m_Creator;
			String fenxiangStr = "";
			if (m_byMaxRound == 6) {
				fenxiangStr += "6局 ";
			} else if (m_byMaxRound == 12) {
				fenxiangStr += "12局 ";
			}
			if ("FZPAY".equals(m_payWay)) {
				fenxiangStr += "房主支付 ";
			} else if ("AAPAY".equals(m_payWay)) {
				fenxiangStr += "AA支付 ";
			} else if ("WINPAY".equals(m_payWay)) {
				fenxiangStr += "赢家支付 ";
			} else if ("CLUBPAY".equals(m_payWay)) {
				fenxiangStr += "圈主支付 ";
			}
			cmd.fenxiangStr = fenxiangStr;
			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
		}

		{
			SUBCMD_S_Table cmd = new SUBCMD_S_Table();
			cmd.m_dwTableID = m_dwTableID;// ����ID
			cmd.m_bTableStarted = m_bTableStarted ? 1 : 0;// ����ID
			cmd.m_bGameStarted = m_bGameStarted ? 1 : 0;// �Ƿ�ʼ
			cmd.m_byYuPai = m_MajPaiQiang.GetYuPaiCount();
			cmd.m_byRound = m_byRound;
			cmd.m_byMaxRound = m_byMaxRound;
			cmd.m_cntLookon = 1;// ����

			Constants.memset(cmd.m_seatInfo, 0, Constants.sizeof(cmd.m_seatInfo));

			for (int i = 0; i < PLAYERCOUNT; i++) {
				cmd.m_seatInfo[i].m_lUid = m_pIUserItem[i].dwUserID;// UID
				cmd.m_seatInfo[i].m_iSeatId = i;// ��λ��ID
				cmd.m_seatInfo[i].m_iTotalCoin = m_vecResultScore[i];

				cmd.m_seatInfo[i].m_NickName = m_pIUserItem[i].szNickName;// NICKNAME
				cmd.m_seatInfo[i].m_iBetCoin = 0;//
				cmd.m_seatInfo[i].m_cbGender = m_pIUserItem[i].cbGender;
				cmd.m_seatInfo[i].m_iAvatar = m_pIUserItem[i].dwFaceID;//
				cmd.m_seatInfo[i].m_cbUserStatus = m_pIUserItem[i].cbUserStatus;// �û�״̬
				cmd.m_seatInfo[i].m_iVipLevel = m_pIUserItem[i].cbVipLevel;// VIP
																			// LEVEL
				cmd.m_seatInfo[i].m_iTitle = 0;// �ƺ�
				cmd.m_seatInfo[i].m_isLookon = 0;// �Ƿ����
				cmd.m_seatInfo[i].szIP = m_pIUserItem[i].szIP;
				cmd.m_seatInfo[i].is_offline = m_pIUserItem[i].cbOnline;
			}

			SendData(_Uid, cmd.getJsonData());
			for (int i = 0; i < PLAYERCOUNT; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SendAvatar(m_pIUserItem[i].dwUserID);
				}
			}
		}

		if (m_bGameStarted) {
			{
				SUBCMD_S_TableBegin cmd = new SUBCMD_S_TableBegin();
				cmd.strParam = m_strParam;
				cmd.byZhuang = m_byZhuang;// �Ƿ�ʼ
				cmd.byYuPai = m_MajPaiQiang.GetYuPaiCount();
				cmd.byRound = m_byRound;
				cmd.byMaxRound = m_byMaxRound;
				cmd.byCurrentUser = m_MajLogic.GetCurrentUser();
				for (int i = 0; i < PLAYERCOUNT; i++) {
					cmd.lScore[i] = m_pIUserItem[i].lUserScore;
					cmd.lRoundScore[i] = m_vecRoundScore[i];
				}
				cmd.byLastOutMj = m_MajLogic.GetLastOutMj();
				SendData(_Uid, cmd.getJsonData());
			}
			{
				for (int i = 0; i < PLAYERCOUNT; i++) {
					if (m_pIUserItem[i].dwUserID == _Uid) {
						MajSide lMajSide = m_MajLogic.GetMajSide(i);
						SUBCMD_S_TableSide cmd = new SUBCMD_S_TableSide();
						cmd.bySeatID = i;
						cmd.byTing = lMajSide.byTing;
						cmd.fangjiang = fanJiang1;
						Constants.memcpy(cmd.Maj, lMajSide.Maj, 23);
						Constants.memcpy(cmd.Hua, lMajSide.Hua, 8);
						Constants.memcpy(cmd.hu, lMajSide.hu);
						Constants.memcpy(cmd.outMj, lMajSide.outMj, 20);
						Constants.memcpy(cmd.byRight, m_byRight[i], 6);
						cmd.fangzi_end = fangzi_end;
						cmd.fangzi = fangzi[i];
						cmd.lastout_mj = m_MajLogic.GetLastOutMj();
						cmd.lastout_seat = m_MajLogic.GetLastUser();

						SendData(_Uid, cmd.getJsonData());
					} else if (m_pIUserItem[i].dwUserID > 0) {
						MajSide lMajSide = m_MajLogic.GetMajSide(i);
						SUBCMD_S_TableSide cmd = new SUBCMD_S_TableSide();
						cmd.bySeatID = i;
						cmd.byTing = lMajSide.byTing;
						cmd.fangjiang = fanJiang1;
						int[] mj = new int[Constants.HAND_MJ_MAX];
						Constants.memcpy(mj, lMajSide.Maj, Constants.HAND_MJ_MAX);
						for (int index = 0; index < mj.length; index++) {
							if (mj[index] != Constants.INVALID_CARD) {
								mj[index] = 0;
							}
						}
						Constants.memcpy(cmd.Maj, mj, Constants.HAND_MJ_MAX);
						Constants.memcpy(cmd.Hua, lMajSide.Hua, 8);
						Constants.memcpy(cmd.hu, lMajSide.hu);
						Constants.memcpy(cmd.outMj, lMajSide.outMj, 20);
						Constants.memcpy(cmd.byRight, m_byRight[i], 6);
						cmd.fangzi_end = fangzi_end;
						cmd.fangzi = fangzi[i];
						cmd.lastout_mj = m_MajLogic.GetLastOutMj();
						cmd.lastout_seat = m_MajLogic.GetLastUser();

						SendData(_Uid, cmd.getJsonData());
					}
				}
			}
			{
				SUBCMD_S_TableEnd cmd = new SUBCMD_S_TableEnd();
				SendData(_Uid, cmd.getJsonData());
			}
			{
				if (maiZhuang != 0) {
					SUBCMD_S_MAIZHUANG_Score cmd = new SUBCMD_S_MAIZHUANG_Score();
					cmd.maizhuang_score = maiZhuangScore;
					SendData(cmd.getJsonData());
				}
			}
		} else {
			int bySeatID = FindSeat(_Uid);
			if (m_byRound > 0 && m_byRound < 8 && m_pIUserItem[bySeatID].cbUserStatus != Constants.US_READY) {
				SUBCMD_S_JIESUAN cmd = new SUBCMD_S_JIESUAN();
				cmd.byRound = m_byRound;
				cmd.byZhuang = m_byZhuang;
				cmd.byLianZhuang = m_byLianZhuang;
				cmd.bCengBao = m_bCengBao ? 1 : 0;
				cmd.fanJiang = fanJiang1;
				cmd.maizhuang_score = maiZhuangScore;
				for (int i = 0; i < PLAYERCOUNT; i++) {
					cmd.userJieSuan[i].lRoundHushu = m_vecRoundHushu[i];
					cmd.userJieSuan[i].lRoundScore = m_vecRoundScore[i];
					cmd.userJieSuan[i].resultScore = m_vecResultScore[i];
					cmd.userJieSuan[i].strHuType = m_vecHuType[i];
					cmd.userJieSuan[i].lRoundHuDesc = m_vecRoundHuDesc[i];
					MajSide l_MajSide = m_MajLogic.GetMajSide(i);
					int hua_count = 0;
					for (int j = 0; j < 8; j++) {
						if (l_MajSide.Hua[j] != Constants.INVALID_CARD) {
							hua_count++;
						}
					}
					cmd.userJieSuan[i].huaCount = hua_count;
					if (m_vecZiMo[i] == 1) {
						cmd.userJieSuan[i].zimo_fangpao = 2;
					} else {
						if (m_fangpao[i] == 1) {
							cmd.userJieSuan[i].zimo_fangpao = 1;
						} else {
							cmd.userJieSuan[i].zimo_fangpao = 0;
						}
					}
					Constants.memcpy(cmd.userJieSuan[i].byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
					if (m_bIsHu[i]
							&& cmd.userJieSuan[i].byHandMj[Constants.HAND_MJ_MAX - 1] == Constants.INVALID_CARD) {
						cmd.userJieSuan[i].byHandMj[Constants.HAND_MJ_MAX - 1] = m_MajLogic.GetLastOutMj();
					}
					Constants.memcpy(cmd.userJieSuan[i].hu, l_MajSide.hu, Constants.sizeof(cmd.userJieSuan[i].hu));
				}
				SendData(_Uid, cmd.getJsonData());
			}
		}
		return true;
	}

	public boolean SendTableData(int wChairID, String sData) {
		// 群发用户
		if (wChairID == Constants.INVALID_CHAIR) {
			for (short i = 0; i < 3; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
						Replay.AddJsonMsg(i, sData);
					} else {

					}
				}
			}

			return true;
		} else {
			if (wChairID >= 3)
				return true;
			if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					Replay.AddJsonMsg(wChairID, sData);
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
				} else {

				}
				return true;
			}
		}

		return false;
	}

	/**
	 * ����������ָ����ͻ���
	 * 
	 * @param wChairID
	 * @param sData
	 * @return
	 */
	public boolean SendData(int wChairID, String sData) {
		// Ⱥ���û�
		if (wChairID == Constants.INVALID_CHAIR) {
			for (short i = 0; i < 3; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						Replay.AddJsonMsg(i, sData);
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
					} else {

					}
				}
			}

			return true;
		} else {
			if (wChairID >= 3 && wChairID > 999) {
				dthall.getInstance().onSend(wChairID, sData);
				int sit = FindSeat(wChairID);
				Replay.AddJsonMsg(sit, sData);
			} else if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
					Replay.AddJsonMsg(wChairID, sData);
				} else {

				}
				return true;
			}
		}

		return false;
	}

	void FaPai_1() {
		if (m_byRound > m_byMaxRound) {
			return;
		}
		
		if(maiZhuang == 0){
			m_MajLogic.Init(Constants.INIT_NEWROUND);
			fanJiang1 = m_MajPaiQiang.GetPai();
			Jiang = JiangUtil.getJiang(fanJiang1);
			for (int i = 0; i < RGCPTable.PLAYERCOUNT; i++) {
				MajSide l_MajSide = m_MajLogic.GetMajSide(i);
				for(int index = 0; index < Constants.HAND_MJ_MAX; index++){
					l_MajSide.Maj[index] = Constants.INVALID_CARD;
				}
				
				for (int by = 0; by < Constants.HAND_MJ_MAX - 1; by++) {
					int byMj = m_MajPaiQiang.GetPai();
					l_MajSide.Maj[by] = byMj;
				}
				if (i == m_byZhuang) {
					l_MajSide.Maj[Constants.HAND_MJ_MAX - 1] = m_MajPaiQiang.GetPai();
				}
			}
			SUBCMD_S_FaPai cmd = new SUBCMD_S_FaPai();
			cmd.byRound = m_byRound;
			cmd.byZhuang = m_byZhuang;
			cmd.byLianZhuang = m_byLianZhuang;
			cmd.byMaxRound = m_byMaxRound;
			cmd.fangjiang = fanJiang1;
			for (int i = 0; i < PLAYERCOUNT; i++) {
				MajSide lMajSide = m_MajLogic.GetMajSide(i);
				lMajSide.SortCard();

				int huaCount = 0;
				for (int j = 0; j < lMajSide.Maj.length; j++) {
					if (lMajSide.Maj[j] >= 34 && lMajSide.Maj[j] <= 38) {
						huaCount++;
					}
				}
				qishou_huacount[i] = huaCount;
				Constants.memcpy(cmd.byMj, lMajSide.Maj, Constants.HAND_MJ_MAX);
				for (int index = 0; index < PLAYERCOUNT; index++) {
					Constants.memcpy(cmd.byHuaPai[index], m_MajLogic.GetMajSide(index).Hua, 8);
				}
				String sData = cmd.getJsonData();
				if (m_pIUserItem[i].isAndroidUser == 0) {
					SendData(m_pIUserItem[i].dwUserID, sData);
				}
			}

			fangzi_end = 1;
			SendYuPai();
			OnUserLong(m_byZhuang);
		}else{
			m_MajLogic.Init(Constants.INIT_NEWROUND);
			fanJiang1 = m_MajPaiQiang.GetPai();
			Jiang = JiangUtil.getJiang(fanJiang1);

			for (int i = 0; i < RGCPTable.PLAYERCOUNT; i++) {
				MajSide l_MajSide = m_MajLogic.GetMajSide(i);
				for (int index = 0; index < Constants.HAND_MJ_MAX; index++) {
					l_MajSide.Maj[index] = Constants.INVALID_CARD;
				}

				for (int by = 0; by < 7; by++) {
					int byMj = m_MajPaiQiang.GetPai();
					l_MajSide.Maj[by] = byMj;
				}
			}
			SUBCMD_S_FaPai_1 cmd = new SUBCMD_S_FaPai_1();
			cmd.byRound = m_byRound;
			cmd.byZhuang = m_byZhuang;
			cmd.byLianZhuang = m_byLianZhuang;
			cmd.byMaxRound = m_byMaxRound;
			cmd.fangjiang = fanJiang1;
			cmd.maizhuang = maiZhuang;
			for (int i = 0; i < PLAYERCOUNT; i++) {
				MajSide lMajSide = m_MajLogic.GetMajSide(i);
				lMajSide.SortCard();
				Constants.memcpy(cmd.byMj, lMajSide.Maj, Constants.HAND_MJ_MAX);
				String sData = cmd.getJsonData();
				if (m_pIUserItem[i].isAndroidUser == 0) {
					SendData(m_pIUserItem[i].dwUserID, sData);
				}
			}
			SendYuPai();
		}
		Replay.SetIndexJushu(m_byRound);
	}

	void FaPai_2() {
		for (int i = 0; i < RGCPTable.PLAYERCOUNT; i++) {
			MajSide l_MajSide = m_MajLogic.GetMajSide(i);
			for (int by = 7; by < Constants.HAND_MJ_MAX - 1; by++) {
				int byMj = m_MajPaiQiang.GetPai();
				l_MajSide.Maj[by] = byMj;
			}
			if (i == m_byZhuang) {
				l_MajSide.Maj[Constants.HAND_MJ_MAX - 1] = m_MajPaiQiang.GetPai();
			}
		}

		SUBCMD_S_FaPai_2 cmd = new SUBCMD_S_FaPai_2();
		cmd.byRound = m_byRound;
		cmd.byZhuang = m_byZhuang;
		cmd.byLianZhuang = m_byLianZhuang;
		cmd.byMaxRound = m_byMaxRound;
		cmd.fangjiang = fanJiang1;
		for (int i = 0; i < PLAYERCOUNT; i++) {
			MajSide lMajSide = m_MajLogic.GetMajSide(i);
			lMajSide.SortCard();

			int huaCount = 0;
			for (int j = 0; j < lMajSide.Maj.length; j++) {
				if (lMajSide.Maj[j] >= 34 && lMajSide.Maj[j] <= 38) {
					huaCount++;
				}
			}
			qishou_huacount[i] = huaCount;
			Constants.memcpy(cmd.byMj, lMajSide.Maj, Constants.HAND_MJ_MAX);
			String sData = cmd.getJsonData();
			if (m_pIUserItem[i].isAndroidUser == 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}

		fangzi_end = 1;
		SendYuPai();
		// ��ʾ��
		OnUserLong(m_byZhuang);
	}

	/**
	 * �Ƿ������
	 * 
	 * @param bySeatID
	 * @return
	 */
	boolean OnUserLong(int bySeatID) {
		if (bySeatID >= PLAYERCOUNT) {
			return false;
		}
		if (m_bGameStarted == false) {
			return false;
		}
		SUBCMD_S_fangzi cmd = new SUBCMD_S_fangzi();
		cmd.bySeatID = bySeatID;

		// ���óɵ�ǰ���
		m_MajLogic.SetCurrentUser(bySeatID);

		for (int byIndex = 0; byIndex < PLAYERCOUNT; byIndex++) {
			if (byIndex == bySeatID) {
				// �����ж�
				MajSide l_TmpMajSide = m_MajLogic.GetMajSide(byIndex);
				int byTempMj[] = new int[Constants.HAND_MJ_MAX];

				Constants.memcpy(byTempMj, l_TmpMajSide.Maj, Constants.HAND_MJ_MAX);
				RGCPHelper.SortCard(byTempMj, Constants.HAND_MJ_MAX);

				MJ_LONGINFO mjAnGang = RGCPHelper.IsAnLong(l_TmpMajSide, byTempMj, fanJiang1);
				cmd.nGang = mjAnGang.GangStyle == 0 ? 0 : 1;
			} else {
				cmd.nGang = 0;
			}
			fangzi[byIndex] = cmd.nGang > 0 ? 1 : 0;
			String sData = cmd.getJsonData();
			SendData(m_pIUserItem[byIndex].dwUserID, sData);
		}
		// û�з���ֱ�Ӳ���
		if (fangzi[bySeatID] == 0) {
			first_buhua(bySeatID, 100);
		}
		return true;
	}

	void first_buhua(int bySeatID, int reason) {
		// ����
		MajSide l_TmpMajSide = m_MajLogic.GetMajSide(bySeatID);
		int hua_count = 0;
		for (int i = 0; i < l_TmpMajSide.Maj.length; i++) {
			if (RGCPHelper.IsHuaPai(l_TmpMajSide.Maj[i])) {
				l_TmpMajSide.AddHua(l_TmpMajSide.Maj[i]);
				int mj = m_MajPaiQiang.GetPai();
				while (RGCPHelper.IsHuaPai(mj)) {
					l_TmpMajSide.AddHua(mj);
					mj = m_MajPaiQiang.GetPai();
				}
				l_TmpMajSide.Maj[i] = mj;
				hua_count++;
			}
		}

		if (reason == 100) {// ���Ӻ󲹻� �ಹһ����
			// �жϷ��ӵ�����
			for (int index = 0; index < l_TmpMajSide.hu.block.length; index++) {
				if (l_TmpMajSide.hu.block[index] != null
						&& l_TmpMajSide.hu.block[index].style == Constants.BLOCK_TIANLong) {
					if (l_TmpMajSide.hu.block[index].count == 4) {
						int mj = m_MajPaiQiang.GetPai();
						while (RGCPHelper.IsHuaPai(mj)) {
							l_TmpMajSide.AddHua(mj);
							mj = m_MajPaiQiang.GetPai();
						}
						b: for (int i = 0; i < l_TmpMajSide.Maj.length; i++) {
							if (l_TmpMajSide.Maj[i] == Constants.INVALID_CARD) {
								l_TmpMajSide.Maj[i] = mj;
								break b;
							}
						}
						hua_count++;
					}
				}
			}
		}
		l_TmpMajSide.SortCard();
		if (hua_count > 0) {
			// ��������
			for (int index = 0; index < PLAYERCOUNT; index++) {
				SUBCMD_S_First_BUHUA cmd_mopai = new SUBCMD_S_First_BUHUA();
				cmd_mopai.bySeatID = bySeatID;

				Constants.memcpy(cmd_mopai.hua, l_TmpMajSide.Hua, 8);
				Constants.memcpy(cmd_mopai.byHandMj, l_TmpMajSide.Maj, Constants.HAND_MJ_MAX);

				if (index == bySeatID) {
					Constants.memcpy(cmd_mopai.byHandMj, l_TmpMajSide.Maj, Constants.HAND_MJ_MAX);
				} else {
					Constants.memcpy(cmd_mopai.byHandMj, l_TmpMajSide.Maj, Constants.HAND_MJ_MAX);
					for (int k = 0; k < Constants.HAND_MJ_MAX; k++) {
						if (cmd_mopai.byHandMj[k] != Constants.INVALID_CARD) {
							cmd_mopai.byHandMj[k] = 0;
						}
					}
				}
				String sData = cmd_mopai.getJsonData();
				SendData(m_pIUserItem[index].dwUserID, sData);
			}
			SendYuPai();
		}

		// �л��û�
		m_MajLogic.SetCurrentUser(RGCPHelper.GetNextUser(bySeatID));
		if (m_MajLogic.GetCurrentUser() != m_byZhuang) {
			OnUserLong(m_MajLogic.GetCurrentUser());
		} else {
			// �Ѿ�ת��һȦ ��ʾׯ�ҳ���
			first_chupai(m_MajLogic.GetCurrentUser());
		}

	}

	void first_chupai(int bySeatID) {
		if (m_bGameStarted == false) {
			return;
		}
		if (bySeatID >= PLAYERCOUNT) {
			return;
		}
		if (bySeatID != m_MajLogic.GetCurrentUser()) {
			return;
		}

		SUBCMD_S_First_ChuPai cmd = new SUBCMD_S_First_ChuPai();
		cmd.bySeatID = bySeatID;

		// ���óɵ�ǰ���
		m_MajLogic.SetCurrentUser(bySeatID);

		for (int byIndex = 0; byIndex < PLAYERCOUNT; byIndex++) {
			if (byIndex == bySeatID) {
				// ���������ж�
				MajSide l_TmpMajSide = m_MajLogic.GetMajSide(byIndex);

				int byTempMj[] = new int[Constants.HAND_MJ_MAX];
				Constants.memcpy(byTempMj, l_TmpMajSide.Maj, Constants.HAND_MJ_MAX);
				RGCPHelper.SortCard(byTempMj, Constants.HAND_MJ_MAX);

				cmd.nHu = (RGCPUtil.is_hu(byTempMj, Constants.INVALID_CARD) ? 1 : 0);
				MJ_GANGINFO mjAnGang = RGCPHelper.IsAnGang(byTempMj);
				MJ_GANGINFO mjZMGang = RGCPHelper.IsZMGang(byTempMj, l_TmpMajSide.hu);
				cmd.nGang = mjAnGang.GangStyle + mjZMGang.GangStyle;
			} else {
				cmd.nGang = 0;
				cmd.nHu = 0;
			}
			m_byRight[byIndex][0] = 0;
			m_byRight[byIndex][1] = 0;
			m_byRight[byIndex][2] = cmd.nGang > 0 ? 1 : 0;
			m_byRight[byIndex][3] = 0;
			m_byRight[byIndex][4] = cmd.nHu;
			String sData = cmd.getJsonData();
			SendData(m_pIUserItem[byIndex].dwUserID, sData);
		}
		fangzi_end = 2;
	}

	void Test() {

	}

	static int byTestIndex = 0;

	void PrintPai() {
	}

	void CleanTempAction() {
		for (int i = 0; i < PLAYERCOUNT; i++) {
			m_tempAction[i].Clean();
		}
	}

	void CleanTempRight(int bySeatID) {

		if (bySeatID >= 0 && bySeatID < PLAYERCOUNT) {
			m_byRight[bySeatID][0] = 0;
			m_byRight[bySeatID][1] = 0;
			m_byRight[bySeatID][2] = 0;
			m_byRight[bySeatID][3] = 0;
			m_byRight[bySeatID][4] = 0;
			m_byRight[bySeatID][5] = 0;
		}
	}

	void ZuoPai() {
	}

	int GetActionRight(int bySeatID) {
		if (m_byRight[bySeatID][4] > 0) {
			return Constants.ACTION_HU;
		}
		if (m_byRight[bySeatID][2] > 0) {
			return Constants.ACTION_GANG;
		}
		if (m_byRight[bySeatID][1] > 0) {
			return Constants.ACTION_PENG;
		}
		if (m_byRight[bySeatID][0] > 0) {
			return Constants.ACTION_CHI;
		}
		return 0;
	}

	// �û�����
	boolean OnUserOutMj(int bySeatID, int byOutMj) {
		if (bySeatID >= PLAYERCOUNT || byOutMj >= 42) {
			return false;
		}
		if (m_bGameStarted == false) {
			return false;
		}
		MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);

		m_wRobotSeconds[bySeatID] = 0L;
		if ((l_MajSide.GetHandMjCount() % 3) != 2) {
			return false;
		}
		CleanTempRight(bySeatID);
		m_MajLogic.SetCurrentAction(0);

		// ��ֵУ��
		for (int byIndex = 0; byIndex < Constants.HAND_MJ_MAX; byIndex++) {
			if (l_MajSide.Maj[byIndex] == byOutMj) {
				// ��Чɾ���齫
				l_MajSide.Maj[byIndex] = Constants.INVALID_CARD;

				l_MajSide.ChuPai(byOutMj);

				l_MajSide.SortCard(Constants.HAND_MJ_MAX);

				break;
			}
		}

		m_MajLogic.SetCurrentUser(bySeatID);
		m_MajLogic.SetLastOutMj(byOutMj);

		// 发送数据结构
		SUBCMD_S_ChuPai cmd = new SUBCMD_S_ChuPai();
		cmd.byActionUser = bySeatID;
		cmd.byOutMj = byOutMj;

		// 发送数据
		for (int byIndex = 0; byIndex < 4; byIndex++) {
			cmd.bHu = 0;
			cmd.bGang = 0;
			cmd.bPeng = 0;
			cmd.bChi = 0;

			MajSide l_MajSideTmp = m_MajLogic.GetMajSide(byIndex);

			if (bySeatID != byIndex) {
				// if (m_MajPaiQiang.IsEnd() != 1) {
				cmd.bHu = RGCPUtil.is_hu(l_MajSideTmp.Maj, byOutMj) ? 1 : 0;
				cmd.bGang = RGCPHelper.IsGang(l_MajSideTmp.Maj, byOutMj).GangStyle;
				cmd.bPeng = (RGCPHelper.IsPeng(l_MajSideTmp.Maj, byOutMj) ? 1 : 0);
				// }
			}

			m_MajLogic.m_byOutMjCount++;

			Constants.memset(cmd.byHandMj, Constants.INVALID_CARD, Constants.HAND_MJ_MAX);
			if (byIndex == bySeatID) {
				Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
			} else {
				Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
				for (int k = 0; k < Constants.HAND_MJ_MAX; k++) {
					if (cmd.byHandMj[k] != Constants.INVALID_CARD) {
						cmd.byHandMj[k] = 0;
					}
				}
			}

			m_byRight[byIndex][0] = cmd.bChi;
			m_byRight[byIndex][1] = cmd.bPeng;
			m_byRight[byIndex][2] = cmd.bGang;
			m_byRight[byIndex][3] = 0;
			m_byRight[byIndex][4] = cmd.bHu;

			String sData = cmd.getJsonData();
			SendData(m_pIUserItem[byIndex].dwUserID, sData);
		}
		CleanTempAction();

		m_MajLogic.SetLastUser(bySeatID);
		// �����в���,�¼�����
		int cntActionRight = 0;
		for (int j = 0; j < PLAYERCOUNT; j++) {
			if (GetActionRight(j) > 0) {
				cntActionRight++;
			}
		}
		if (cntActionRight == 0) {
			// �л��û�
			m_MajLogic.SetCurrentUser(RGCPHelper.GetNextUser(bySeatID));
			OnUserTouchMj(m_MajLogic.GetCurrentUser());
		}

		return true;
	}

	int random(int iMin, int iMax) {
		Random ra = new Random();
		return ra.nextInt(iMax - iMin) + iMin;
	}

	// �û�����
	boolean OnUserTouchMj(int bySeatID) {
		return OnUserTouchMj(bySeatID, 0);
	}

	int cnt_step = 0;

	public static int getHuaCount(int[] hua) {
		if (hua == null) {
			return 0;
		}
		int ret = 0;
		for (int i = 0; i < hua.length; i++) {
			if (hua[i] != Constants.INVALID_CARD) {
				ret++;
			}
		}
		return ret;
	}

	/**
	 * �û�����
	 * 
	 * @param bySeatID
	 *            �û���λID
	 * @param cbReason
	 *            ����ԭ��(Ĭ��:0 �ܺ����ƣ�Constants.ACTION_GANG_BUPAI 11)
	 * @return
	 */
	boolean OnUserTouchMj(int bySeatID, int cbReason) {
		if (bySeatID >= PLAYERCOUNT) {
			return false;
		}
		if (m_bGameStarted == false) {
			return false;
		}
		MajSide l_MajSide1 = m_MajLogic.GetMajSide(bySeatID);

		SUBCMD_S_MoPai cmd = new SUBCMD_S_MoPai();
		cmd.reason = cbReason;
		cmd.bySeatID = bySeatID;

		// ���óɵ�ǰ���
		m_MajLogic.SetCurrentUser(bySeatID);

		int byMoPai = Constants.INVALID_CARD;
		switch (cbReason) {
		case Constants.ACTION_GANG_BUPAI:
			byMoPai = m_MajPaiQiang.GetGangMoPai();
			break;
		case Constants.ACTION_BU_HUA:
			byMoPai = m_MajPaiQiang.GetBuHuaMoPai();
			break;
		default:
			byMoPai = m_MajPaiQiang.GetPai();
			break;
		}

		if (byMoPai >= 42) {// ��Ϸ���� ����
			for (int i = 0; i < PLAYERCOUNT; i++) {
				// ����δ�����ߵĵ÷�
				int fail_huaCount = 0;
				for (int index = 0; index < 8; index++) {
					if (m_MajLogic.GetMajSide(i).Hua[index] != Constants.INVALID_CARD) {
						fail_huaCount++;
					}
				}
				// ��ȡ���ܵ�����
				// ��ȡ���ܵ�����
				int angang_count = 0;
				int sanlaoCount = 0;
				List<HuBlock> pengBlockList = new ArrayList<HuBlock>();
				for (int j = 0; j < m_MajLogic.GetMajSide(i).hu.block.length; j++) {
					if (m_MajLogic.GetMajSide(i).hu.block[j].style == Constants.BLOCK_AN_GANG) {
						angang_count++;
						if (m_MajLogic.GetMajSide(i).hu.block[j].first == 27
								|| m_MajLogic.GetMajSide(i).hu.block[j].first == 28
								|| m_MajLogic.GetMajSide(i).hu.block[j].first == 29) {
							sanlaoCount++;
						}
					}
					if (m_MajLogic.GetMajSide(i).hu.block[j].style == Constants.BLOCK_TIANLong) {
						angang_count++;
						if (m_MajLogic.GetMajSide(i).hu.block[j].first == 27
								|| m_MajLogic.GetMajSide(i).hu.block[j].first == 28
								|| m_MajLogic.GetMajSide(i).hu.block[j].first == 29) {
							sanlaoCount++;
						}
					}
					if (m_MajLogic.GetMajSide(i).hu.block[j].style == Constants.BLOCK_PENG) {
						pengBlockList.add(new HuBlock(m_MajLogic.GetMajSide(i).hu.block[j].first, BlockType.PENG));
						if (m_MajLogic.GetMajSide(i).hu.block[j].first == 27
								|| m_MajLogic.GetMajSide(i).hu.block[j].first == 28
								|| m_MajLogic.GetMajSide(i).hu.block[j].first == 29) {
							sanlaoCount++;
						}
					}
					if (m_MajLogic.GetMajSide(i).hu.block[j].style == Constants.BLOCK_GANG) {
						if (m_MajLogic.GetMajSide(i).hu.block[j].first == 27
								|| m_MajLogic.GetMajSide(i).hu.block[j].first == 28
								|| m_MajLogic.GetMajSide(i).hu.block[j].first == 29) {
							sanlaoCount++;
						}
					}
				}
				Map<String, Object> huMap = RGCPUtil.getFailHuScore(m_MajLogic.GetMajSide(i).Maj, huScore[i], fail_huaCount,
						f_shuanglong, angang_count, fanJiang1, qishou_huacount[i], Jiang, f_sanlaohuimian, sanlaoCount,
						pengBlockList);
				Integer huScore = (Integer) huMap.get("huScore");
				String huDesc = (String) huMap.get("huDesc");
				m_vecRoundHushu[i] = huScore;
				m_vecRoundHuDesc[i] = huDesc;
			}

			int roundScore[] = new int[PLAYERCOUNT];
			for (int i = 0; i < PLAYERCOUNT; i++) {
				int scoreResult = 0;
				for (int j = 0; j < PLAYERCOUNT; j++) {
					if (i != j) {
						if (maiZhuang == 0) {
							scoreResult += m_vecRoundHushu[i] - m_vecRoundHushu[j];
						} else if (maiZhuang == 1) {
							if (maiZhuangScore[i] == 1 && maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 3;
							} else if (maiZhuangScore[i] == 1 || maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 2;
							} else {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 1;
							}
						} else if (maiZhuang == 2) {
							if (maiZhuangScore[i] == 1 && maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 4;
							} else if (maiZhuangScore[i] == 1 || maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 3;
							} else {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 2;
							}
						} else if (maiZhuang == 3) {
							if (maiZhuangScore[i] == 1 && maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 5;
							} else if (maiZhuangScore[i] == 1 || maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 4;
							} else {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 3;
							}
						}
					}
				}
				roundScore[i] = scoreResult;
			}

			for (int k = 0; k < PLAYERCOUNT; k++) {
				m_vecRoundScore[k] = roundScore[k];
				m_vecResultScore[k] += roundScore[k];
			}
			for (int i = 0; i < PLAYERCOUNT; i++) {
				m_vecZiMo[i] = 0;
				m_fangpao[i] = 0;
			}
			OnEventGameEnd(bySeatID, Constants.GAME_END_PASS);
			m_byZhuang = (m_byZhuang + 1) % PLAYERCOUNT;
			return true;
		}
		for (int byIndex = 0; byIndex < PLAYERCOUNT; byIndex++) {
			Constants.memset(cmd.byHandMj, Constants.INVALID_CARD, Constants.HAND_MJ_MAX);
			if (byIndex == bySeatID) {

				cmd.byMoPai = byMoPai;
				MajSide l_TmpMajSide = m_MajLogic.GetMajSide(byIndex);
				l_TmpMajSide.SetMoPai(byMoPai);
				if (RGCPHelper.IsHuaPai(byMoPai)) {
					cmd.nGang = 0;
					cmd.nHu = 0;
				} else {

					int byTempMj[] = new int[Constants.HAND_MJ_MAX];
					Constants.memcpy(byTempMj, l_TmpMajSide.Maj, Constants.HAND_MJ_MAX);
					RGCPHelper.SortCard(byTempMj, Constants.HAND_MJ_MAX);
					cmd.nHu = RGCPUtil.is_hu(byTempMj, Constants.INVALID_CARD) ? 1 : 0;
					MJ_GANGINFO mjAnGang = RGCPHelper.IsAnGang(byTempMj);
					MJ_GANGINFO mjZMGang = RGCPHelper.IsZMGang(byTempMj, l_TmpMajSide.hu);
					cmd.nGang = mjAnGang.GangStyle + mjZMGang.GangStyle;
				}
			} else {
				cmd.nGang = 0;
				cmd.nHu = 0;
				if (RGCPHelper.IsHuaPai(byMoPai)) {
					cmd.byMoPai = byMoPai;
				} else {
					cmd.byMoPai = Constants.INVALID_CARD; // ��Ч��
				}
			}
			m_byRight[byIndex][0] = 0;
			m_byRight[byIndex][1] = 0;
			m_byRight[byIndex][2] = cmd.nGang > 0 ? 1 : 0;
			m_byRight[byIndex][3] = cmd.bTing;
			m_byRight[byIndex][4] = cmd.nHu;
			Constants.memset(cmd.byHandMj, Constants.INVALID_CARD, Constants.HAND_MJ_MAX);
			if (byIndex == bySeatID) {
				Constants.memcpy(cmd.byHandMj, l_MajSide1.Maj, Constants.HAND_MJ_MAX);
			} else {
				Constants.memcpy(cmd.byHandMj, l_MajSide1.Maj, Constants.HAND_MJ_MAX);
				for (int k = 0; k < Constants.HAND_MJ_MAX; k++) {
					if (cmd.byHandMj[k] != Constants.INVALID_CARD) {
						cmd.byHandMj[k] = 0;
					}
				}
				cmd.byHandMj[Constants.HAND_MJ_MAX - 1] = 0;
			}

			String sData = cmd.getJsonData();
			SendData(m_pIUserItem[byIndex].dwUserID, sData);
		}

		SendYuPai();
		if (RGCPHelper.IsHuaPai(byMoPai)) {
			OnUserBuHua(bySeatID);
		}
		return true;
	}

	boolean OnUserBuHua(int bySeatID) {
		if (bySeatID >= PLAYERCOUNT) {
			return false;
		}
		if (m_bGameStarted == false) {
			return false;
		}
		m_MajLogic.SetCurrentAction(Constants.ACTION_BU_HUA);
		MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
		int byMoPai = l_MajSide.GetMoPai();
		if (RGCPHelper.IsHuaPai(byMoPai)) {
			l_MajSide.AddHua(byMoPai);
			l_MajSide.SetMoPai(Constants.INVALID_CARD);

			SUBCMD_S_BuHua cmd = new SUBCMD_S_BuHua();
			cmd.bySeatID = bySeatID;
			cmd.byHua = byMoPai;
			SendData(Constants.INVALID_CARD, cmd.getJsonData());

			OnUserTouchMj(bySeatID, Constants.ACTION_BU_HUA);
		}
		return true;
	}

	// �û�����
	boolean OnUserGiveUp(int bySeatID, SUBCMD_C_Operation Operation) {
		if (bySeatID >= 4) {
			return false;
		}
		if (m_bGameStarted == false) {
			return false;
		}
		
		if (GetActionRight(bySeatID)  == 0) {
			return false;
		}
		
		m_MajLogic.SetCurrentAction(Constants.ACTION_GIVE_UP);
		// ��������

		CleanTempRight(bySeatID);

		// �ǵ�ǰ���
		if (m_MajLogic.GetMajSide(bySeatID).Maj[Constants.HAND_MJ_MAX - 1] == Constants.INVALID_CARD) {
			int tempRight = 0;
			for (int i = 0; i < PLAYERCOUNT; i++) {
				if (GetActionRight(i) > tempRight) {
					tempRight = GetActionRight(i);
				}
			}
			if (tempRight > 0) {
				int bySeat1 = RGCPHelper.GetNextUser(m_MajLogic.GetCurrentUser());
				int bySeat2 = RGCPHelper.GetNextUser(bySeat1);

				int bySeatOrder[] = new int[PLAYERCOUNT];
				bySeatOrder[0] = m_MajLogic.GetCurrentUser();
				bySeatOrder[1] = bySeat1;
				bySeatOrder[2] = bySeat2;
				for (int i = 0; i < PLAYERCOUNT; i++) {
					if (bySeatOrder[i] == bySeatID || bySeatOrder[i] == m_MajLogic.GetCurrentUser())
						continue;
					switch (m_tempAction[bySeatOrder[i]].m_ActionRight) {
					case Constants.ACTION_PENG:
						OnUserPeng(bySeatOrder[i], m_tempAction[bySeatOrder[i]].m_cPeng);
						break;
					case Constants.ACTION_GANG:
						OnUserGang(bySeatOrder[i], m_tempAction[bySeatOrder[i]].m_cGang);
						break;
					case Constants.ACTION_HU:
						OnUserHu(bySeatOrder[i], m_tempAction[bySeatOrder[i]].m_cHu);
						break;
					default:
						break;
					}
				}
			} else {
				// ȫ���˶���������,�¼�����
				int byNextUser = RGCPHelper.GetNextUser(m_MajLogic.GetCurrentUser());
				OnUserTouchMj(byNextUser);
			}
		}

		return true;
	}

	// �û���
	public void Handle_onLongPai(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == INVALID_CHAIR) {
			return;
		}
		if (bySeatID >= PLAYERCOUNT) {
			return;
		}
		if (m_bGameStarted == false) {
			return;
		}
		int byLongMj = atoi(vecArg[1]);

		// �޸ܲ���Ȩ�����ϵĲ���
		MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
		MJ_BLOCK block = (MJ_BLOCK) RGCPHelper.AnLong(l_MajSide.Maj, byLongMj);
		RGCPHelper.AddBlock(l_MajSide.hu, block);
		m_MajLogic.GetMajSide(bySeatID).SortCard(Constants.HAND_MJ_MAX);
		huScore[bySeatID] = huScore[bySeatID] + JiangUtil.getLongScore(fanJiang1, Jiang, block.first);
		m_MajLogic.SetCurrentAction(Constants.ACTION_Long);
		// ��Ϣ����
		SUBCMD_S_longCL cmd = new SUBCMD_S_longCL();
		cmd.block = block;
		cmd.byActionUser = bySeatID;
		cmd.gangType = 5;
		cmd.byMj = block.first;
		cmd.byBuMj = 255;
		cmd.bZiMoGang = 0;
		cmd.bTing = 0;
		Constants.memset(cmd.bHu, 0, Constants.sizeof(cmd.bHu));
		for (int j = 0; j < PLAYERCOUNT; j++) {
			Constants.memset(cmd.byHandMj, Constants.INVALID_CARD, Constants.HAND_MJ_MAX);
			if (j == bySeatID) {
				Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
			} else {
				Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
				for (int k = 0; k < Constants.HAND_MJ_MAX; k++) {
					if (cmd.byHandMj[k] != Constants.INVALID_CARD) {
						cmd.byHandMj[k] = 0;
					}
				}
			}

			String sData = cmd.getJsonData();
			SendData(m_pIUserItem[j].dwUserID, sData);
		}
		CleanTempAction();
		fangzi[bySeatID] = 0;
		// ������Ϣ
		// ����������
		OnUserLong(bySeatID);
		// first_buhua(bySeatID, 100);
		return;

	}

	// �û�����
	boolean OnUserPeng(int bySeatID, SUBCMD_C_Operation Peng) {
		if (bySeatID >= PLAYERCOUNT || Peng == null) {
			return false;
		}

		int byChuPaiUser = m_MajLogic.GetCurrentUser();
		MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
		MajSide l_ChuPaiMajSide = m_MajLogic.GetMajSide(byChuPaiUser);

		if ((l_MajSide.GetHandMjCount() % 3) != 1) {
			return false;
		}
		// У����û����Ȩ��
		if (GetActionRight(bySeatID) < Constants.ACTION_PENG) {
			return false;
		}

		m_MajLogic.SetCurrentAction(Constants.ACTION_PENG);

		int byLastOutMj = m_MajLogic.GetLastOutMj();

		// ��������Ȩ�����ϵĲ���
		int userCount = 0; // ��������
		for (int i = 0; i < PLAYERCOUNT; i++) {
			// ��������,�ͳ�����
			if (i == bySeatID || i == m_MajLogic.GetCurrentUser())
				continue;

			if (GetActionRight(i) > Constants.ACTION_PENG) {
				m_tempAction[bySeatID].m_ActionRight = Constants.ACTION_PENG;
				m_tempAction[bySeatID].m_cPeng = Peng;
				CleanTempRight(bySeatID);
				m_byRight[bySeatID][1] = 1;
				break;
			} else {// ������
				userCount++;
				CleanTempRight(i);
				if (userCount == 1) {
					CleanTempRight(bySeatID);
					CleanTempAction();
					// ������
					MJ_BLOCK block;
					block = (MJ_BLOCK) RGCPHelper.Peng(l_MajSide.Maj, m_MajLogic.GetLastOutMj());
					RGCPHelper.AddBlock(l_MajSide.hu, block);
					RGCPHelper.SortCard(l_MajSide.Maj, Constants.HAND_MJ_MAX);

					huScore[bySeatID] = huScore[bySeatID] + JiangUtil.getPengScore(fanJiang1, Jiang, block.first);

					// ������Ϣ
					SUBCMD_S_Peng cmd = new SUBCMD_S_Peng();
					cmd.block.first = block.first;
					cmd.block.style = block.style;
					cmd.byActionUser = bySeatID;
					cmd.byMj = byLastOutMj;
					cmd.byOutMjUser = m_MajLogic.GetCurrentUser();

					l_ChuPaiMajSide.BeiChiPai();
					// ��ǰ�û��任
					m_MajLogic.SetCurrentUser(bySeatID);
					for (int j = 0; j < PLAYERCOUNT; j++) {
						Constants.memset(cmd.byHandMj, Constants.INVALID_CARD, Constants.HAND_MJ_MAX);
						if (j == bySeatID) {
							Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
						} else {
							Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
							for (int k = 0; k < Constants.HAND_MJ_MAX; k++) {
								if (cmd.byHandMj[k] != Constants.INVALID_CARD) {
									cmd.byHandMj[k] = 0;
								}
							}
						}
						String sData = cmd.getJsonData();
						SendData(m_pIUserItem[j].dwUserID, sData);
					}
					m_MajLogic.SetLastOutMj(Constants.INVALID_CARD);
					break;
				}
			}
		}
		SendYuPai();
		return true;
	}

	/**
	 * ��ȡ�����ߵĺ���
	 * 
	 * @param bySeatID
	 * @return
	 */
	private Map<String, Object> getHuScore(int bySeatID) {
		MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
		int[] handMj = l_MajSide.Maj;
		boolean is_zimo = false;
		int byLastOutUser = m_MajLogic.GetCurrentUser();
		if (bySeatID == byLastOutUser) {
			is_zimo = true;
		}
		int angang_count = 0;
		int minggang_count = 0;
		int peng_count = 0;
		int feiJiang_peng_count = 0;// �ǽ���������
		int huaCount = 0;
		for (int i = 0; i < l_MajSide.hu.block.length; i++) {
			if (l_MajSide.hu.block[i].style == Constants.BLOCK_AN_GANG
					|| l_MajSide.hu.block[i].style == Constants.BLOCK_TIANLong) {
				angang_count++;
			} else if (l_MajSide.hu.block[i].style == Constants.BLOCK_PENG) {
				peng_count++;
				if (!JiangUtil.is_jiang(l_MajSide.hu.block[i].first, Jiang)) {
					feiJiang_peng_count++;
				}
			} else if (l_MajSide.hu.block[i].style == Constants.BLOCK_GANG) {
				minggang_count++;
			}
		}
		for (int i = 0; i < l_MajSide.Hua.length; i++) {
			if (l_MajSide.Hua[i] != Constants.INVALID_CARD) {
				huaCount++;
			}
		}
		int dihuScore = huScore[bySeatID];
		int qishou_hua_count = qishou_huacount[bySeatID];
		int sanlao = 0;
		for (int i = 0; i < l_MajSide.hu.block.length; i++) {
			if (l_MajSide.hu.block[i].first == 27 || l_MajSide.hu.block[i].first == 28
					|| l_MajSide.hu.block[i].first == 29) {
				sanlao++;
			}
		}
		int yuPaiCount = m_MajPaiQiang.GetYuPaiCount();
		int last_mj = Constants.INVALID_CARD;
		if (is_zimo) {
			last_mj = l_MajSide.Maj[Constants.HAND_MJ_MAX - 1];
		} else {
			last_mj = m_MajLogic.GetLastOutMj();
		}

		List<HuBlock> pengBlockList = new ArrayList<HuBlock>();
		for (int i = 0; i < l_MajSide.hu.block.length; i++) {
			if (l_MajSide.hu.block[i].style == Constants.BLOCK_PENG) {
				pengBlockList.add(new HuBlock(l_MajSide.hu.block[i].first, BlockType.PENG));
			}
		}

		Map<String, Object> qinghuMap = RGCPUtil.getQingHuScore(handMj, last_mj, is_zimo, angang_count, minggang_count,
				peng_count, feiJiang_peng_count, huaCount, dihuScore, fanJiang1, Jiang, qishou_hua_count, sanlao,
				f_sanlaohuimian, f_shuanglong, f_haidilaoyue, yuPaiCount, is_xier);

		Map<String, Object> shisanhuMap = RGCPUtil.getShiSanHuScore(handMj, last_mj, is_zimo, angang_count,
				minggang_count, peng_count, feiJiang_peng_count, huaCount, dihuScore, fanJiang1, Jiang,
				qishou_hua_count, sanlao, f_sanlaohuimian, f_shuanglong, f_haidilaoyue, yuPaiCount, is_xier,
				pengBlockList);

		Map<String, Object> qiyuhuMap = RGCPUtil.getQiyuHuScore(handMj, last_mj, is_zimo, angang_count, minggang_count,
				peng_count, feiJiang_peng_count, huaCount, dihuScore, fanJiang1, Jiang, qishou_hua_count, sanlao,
				f_sanlaohuimian, f_shuanglong, f_haidilaoyue, yuPaiCount, is_xier, pengBlockList);

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		if (qinghuMap != null) {
			list.add(qinghuMap);
		}
		if (shisanhuMap != null) {
			list.add(shisanhuMap);
		}
		if (qiyuhuMap != null) {
			list.add(qiyuhuMap);
		}
		return RGCPUtil.getMaxScore(list);
	}

	boolean OnUserHu(int bySeatID, SUBCMD_C_Hu hu) {
		if (bySeatID >= 4) {
			return false;
		}
		if (m_bGameStarted == false) {
			return false;
		}
		int byLastOutUser = m_MajLogic.GetCurrentUser();
		// Ȩ��Ч��
		if (GetActionRight(bySeatID) < Constants.ACTION_HU && m_byRight[bySeatID][4] == 0) {
			return false;
		}
		int byNextUser1 = RGCPHelper.GetNextUser(byLastOutUser);
		int byNextUser2 = RGCPHelper.GetNextUser(byNextUser1);
		if (bySeatID == byNextUser1) {

		}
		if (bySeatID == byNextUser2) {
			if (GetActionRight(byNextUser1) == Constants.ACTION_HU) {
				m_tempAction[bySeatID].m_ActionRight = Constants.ACTION_HU;
				m_tempAction[bySeatID].m_cHu = hu;
				CleanTempRight(bySeatID);
				m_byRight[bySeatID][4] = 1;
				return false;
			}
		}
		HU temphu = new HU();
		RGCPHelper.CleanHu(temphu);
		MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
		l_MajSide.SortCard();
		// ���������
		int byActivePai = (bySeatID == byLastOutUser) ? Constants.INVALID_CARD : m_MajLogic.GetLastOutMj();

		if (RGCPUtil.is_hu(l_MajSide.Maj, byActivePai)) {
			// ��������ߵĵ÷�
			Map<String, Object> map = getHuScore(bySeatID);
			Integer hushu = (Integer) map.get("huScore");
			Integer huType = (Integer) map.get("huType");
			String huDesc = (String) map.get("huDesc");
			m_vecRoundHushu[bySeatID] = hushu;
			m_vecHuType[bySeatID] = huType;
			m_vecRoundHuDesc[bySeatID] = huDesc;

			for (int i = 0; i < PLAYERCOUNT; i++) {
				if (i != bySeatID) {
					// ����δ�����ߵĵ÷�
					int fail_huaCount = 0;
					for (int index = 0; index < 8; index++) {
						if (m_MajLogic.GetMajSide(i).Hua[index] != Constants.INVALID_CARD) {
							fail_huaCount++;
						}
					}
					// ��ȡ���ܵ�����
					int angang_count = 0;
					int sanlaoCount = 0;
					List<HuBlock> pengBlockList = new ArrayList<HuBlock>();
					for (int j = 0; j < m_MajLogic.GetMajSide(i).hu.block.length; j++) {
						if (m_MajLogic.GetMajSide(i).hu.block[j].style == Constants.BLOCK_AN_GANG) {
							angang_count++;
							if (m_MajLogic.GetMajSide(i).hu.block[j].first == 27
									|| m_MajLogic.GetMajSide(i).hu.block[j].first == 28
									|| m_MajLogic.GetMajSide(i).hu.block[j].first == 29) {
								sanlaoCount++;
							}
						}
						if (m_MajLogic.GetMajSide(i).hu.block[j].style == Constants.BLOCK_TIANLong) {
							angang_count++;
							if (m_MajLogic.GetMajSide(i).hu.block[j].first == 27
									|| m_MajLogic.GetMajSide(i).hu.block[j].first == 28
									|| m_MajLogic.GetMajSide(i).hu.block[j].first == 29) {
								sanlaoCount++;
							}
						}
						if (m_MajLogic.GetMajSide(i).hu.block[j].style == Constants.BLOCK_PENG) {
							pengBlockList.add(new HuBlock(m_MajLogic.GetMajSide(i).hu.block[j].first, BlockType.PENG));
							if (m_MajLogic.GetMajSide(i).hu.block[j].first == 27
									|| m_MajLogic.GetMajSide(i).hu.block[j].first == 28
									|| m_MajLogic.GetMajSide(i).hu.block[j].first == 29) {
								sanlaoCount++;
							}
						}
						if (m_MajLogic.GetMajSide(i).hu.block[j].style == Constants.BLOCK_GANG) {
							if (m_MajLogic.GetMajSide(i).hu.block[j].first == 27
									|| m_MajLogic.GetMajSide(i).hu.block[j].first == 28
									|| m_MajLogic.GetMajSide(i).hu.block[j].first == 29) {
								sanlaoCount++;
							}
						}
					}
					
					Map<String, Object> huMap = RGCPUtil.getFailHuScore(m_MajLogic.GetMajSide(i).Maj, huScore[i], fail_huaCount,
							f_shuanglong, angang_count, fanJiang1, qishou_huacount[i], Jiang, f_sanlaohuimian, sanlaoCount,
							pengBlockList);
					Integer huScore = (Integer) huMap.get("huScore");
					m_vecRoundHushu[i] = huScore;
					m_vecRoundHuDesc[i] = (String) huMap.get("huDesc");
				}
			}

			int bySeat1 = RGCPHelper.GetNextUser(byLastOutUser);
			int bySeat2 = RGCPHelper.GetNextUser(bySeat1);
			if (bySeatID == bySeat1 || (bySeatID == bySeat2 && GetActionRight(bySeat1) < Constants.ACTION_HU)) {
				for (int j = 0; j < PLAYERCOUNT; j++) {
					if (GetActionRight(j) < Constants.ACTION_HU) {
						CleanTempRight(j);
					}
				}
				CleanTempAction();

				CleanTempRight(bySeatID);
			} else {
				// �޺�����Ȩ�����ϵĲ���
				int userCount = 0; // ��������
				for (int i = 0; i < PLAYERCOUNT; i++) {
					// ��������,�ͳ�����
					if (i == bySeatID || i == byLastOutUser)
						continue;

					if (GetActionRight(i) == Constants.ACTION_HU) {

						m_tempAction[bySeatID].m_ActionRight = Constants.ACTION_HU;
						m_tempAction[bySeatID].m_cHu = hu;
						break;
					}
					// ���ƴ���
					userCount++;
					CleanTempRight(i);
					if (userCount == 2) {
						CleanTempRight(bySeatID);
						CleanTempAction();
					}
				}
				if (GetActionRight(bySeatID) == Constants.ACTION_HU) {
					CleanTempRight(bySeatID);
				}
			}

			CleanTempAction();

			SUBCMD_S_HU cmd = new SUBCMD_S_HU();

			cmd.byWinner = bySeatID;

			cmd.byHuMj = byActivePai;

			int roundScore[] = new int[PLAYERCOUNT];
			for (int i = 0; i < PLAYERCOUNT; i++) {
				int scoreResult = 0;
				for (int j = 0; j < PLAYERCOUNT; j++) {
					if (i != j) {
						if (maiZhuang == 0) {
							scoreResult += m_vecRoundHushu[i] - m_vecRoundHushu[j];
						} else if (maiZhuang == 1) {
							if (maiZhuangScore[i] == 1 && maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 3;
							} else if (maiZhuangScore[i] == 1 || maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 2;
							} else {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 1;
							}
						} else if (maiZhuang == 2) {
							if (maiZhuangScore[i] == 1 && maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 4;
							} else if (maiZhuangScore[i] == 1 || maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 3;
							} else {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 2;
							}
						} else if (maiZhuang == 3) {
							if (maiZhuangScore[i] == 1 && maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 5;
							} else if (maiZhuangScore[i] == 1 || maiZhuangScore[j] == 1) {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 4;
							} else {
								scoreResult += (m_vecRoundHushu[i] - m_vecRoundHushu[j]) * 3;
							}
						}
					}
				}
				roundScore[i] = scoreResult;
			}

			for (int k = 0; k < PLAYERCOUNT; k++) {
				m_vecRoundScore[k] = roundScore[k];
				m_vecResultScore[k] += roundScore[k];
			}

			for (int k = 0; k < PLAYERCOUNT; k++) {
				m_pIUserItem[k].lUserScore += roundScore[k];
				cmd.winScore[k] = roundScore[k];
				cmd.resultScore[k] = m_pIUserItem[k].lUserScore;

				if (roundScore[k] > 0) {
					m_vecWinScore[k] += roundScore[k];
				} else {
					m_vecLoseScore[k] += roundScore[k];
				}
			}
			for (int i = 0; i < PLAYERCOUNT; i++) {
				m_vecZiMo[i] = 0;
				m_fangpao[i] = 0;
			}
			m_vecZiMo[bySeatID] = byActivePai == Constants.INVALID_CARD ? 1 : 0;
			if (m_vecZiMo[bySeatID] == 0) {
				m_fangpao[byLastOutUser] = 1;
			}
			m_kReport[bySeatID].cntHu++;
			if (byActivePai == Constants.INVALID_CARD) {
				m_kReport[bySeatID].cntZiMo++;
			} else {
				m_kReport[byLastOutUser].cntDianPao++;
			}

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			m_bIsHu[bySeatID] = true;

			int cntHuRight = 0; // ��������
			for (int i = 0; i < PLAYERCOUNT; i++) {
				// ��������,�ͳ�����
				if (i == bySeatID || i == byLastOutUser)
					continue;

				if (GetActionRight(i) == Constants.ACTION_HU) {
					cntHuRight++;
					break;
				}
			}

			if (bySeatID == byNextUser1) {
				cntHuRight = 0;
			}
			if (bySeatID == byNextUser2 && GetActionRight(byNextUser1) != Constants.ACTION_HU) {
				cntHuRight = 0;
			}
			if (cntHuRight == 0) {
				CleanTempAction();
				OnEventGameEnd(bySeatID, Constants.GAME_END_NORMAL);
			}
			m_byZhuang = (m_byZhuang + 1) % PLAYERCOUNT;
		}

		SendYuPai();
		return true;
	}

	// �û�����
	boolean OnUserGang(int bySeatID, SUBCMD_C_Gang Gang) {
		if (bySeatID >= PLAYERCOUNT || Gang == null) {
			return false;
		}
		if (m_bGameStarted == false) {
			return false;
		}
		// Ȩ��Ч��
		if (GetActionRight(bySeatID) < Constants.ACTION_GANG) {
			return false;
		}
		// �޸ܲ���Ȩ�����ϵĲ���
		int userCount = 0; // ��������
		for (int i = 0; i < PLAYERCOUNT; i++) {
			// ��������,�ͳ�����
			if (i == bySeatID || i == m_MajLogic.GetCurrentUser())
				continue;

			if (GetActionRight(i) > Constants.ACTION_GANG) {
				m_tempAction[bySeatID].m_ActionRight = Constants.ACTION_GANG;
				m_tempAction[bySeatID].m_cGang = Gang;
				CleanTempRight(bySeatID);
				m_byRight[bySeatID][2] = 1;
				break;
			} else {// �ܴ���
				userCount++;
				CleanTempRight(i);
				if (userCount == 1) {
					switch (Gang.nGangMode) {
					case 1: // ��ͨ��
					{
						CleanTempRight(bySeatID);
						MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
						MJ_BLOCK block;
						block = (MJ_BLOCK) RGCPHelper.MGang(l_MajSide.Maj, m_MajLogic.GetLastOutMj());
						RGCPHelper.AddBlock(l_MajSide.hu, block);
						huScore[bySeatID] = huScore[bySeatID]
								+ JiangUtil.getMingGangScore(fanJiang1, Jiang, block.first);

						m_MajLogic.SetCurrentAction(Constants.ACTION_GANG);
						// ���͸�����Ϣ
						SUBCMD_S_Gang cmd = new SUBCMD_S_Gang();
						cmd.byActionUser = bySeatID;
						cmd.byOutMjUser = m_MajLogic.GetCurrentUser();
						cmd.byMj = block.first;
						cmd.block = block;
						cmd.gangType = 1;
						cmd.byBuMj = 255;

						cmd.bZiMoGang = 0;
						Constants.memset(cmd.bHu, 0, Constants.sizeof(cmd.bHu));

						CleanTempAction();

						m_MajLogic.GetMajSide(m_MajLogic.GetCurrentUser()).BeiChiPai();
						// ��ǰ�û��任
						m_MajLogic.SetCurrentUser(bySeatID);

						for (int j = 0; j < PLAYERCOUNT; j++) {
							Constants.memset(cmd.byHandMj, Constants.INVALID_CARD, Constants.HAND_MJ_MAX);
							if (j == bySeatID) {
								Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
							} else {
								Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
								for (int k = 0; k < Constants.HAND_MJ_MAX; k++) {
									if (cmd.byHandMj[k] != Constants.INVALID_CARD) {
										cmd.byHandMj[k] = 0;
									}
								}
							}
							String sData = cmd.getJsonData();
							SendData(m_pIUserItem[j].dwUserID, sData);
						}
						m_MajLogic.SetLastOutMj(Constants.INVALID_CARD);
						// ����������
						OnUserTouchMj(bySeatID, Constants.ACTION_GANG_BUPAI);
					}
						break;
					case 2: // ����
					{
						CleanTempRight(bySeatID);
						MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
						RGCPHelper.ZMGang(l_MajSide.Maj, l_MajSide.hu, Gang.byGangMj);
						m_MajLogic.GetMajSide(bySeatID).SortCard(Constants.HAND_MJ_MAX);

						huScore[bySeatID] = huScore[bySeatID] - JiangUtil.getPengScore(fanJiang1, Jiang, Gang.byGangMj);
						huScore[bySeatID] = huScore[bySeatID]
								+ JiangUtil.getMingGangScore(fanJiang1, Jiang, Gang.byGangMj);

						m_MajLogic.SetCurrentAction(Constants.ACTION_GANG);
						MJ_BLOCK block = new MJ_BLOCK();
						block.style = Constants.BLOCK_GANG;
						block.first = Gang.byGangMj;
						// ���͸�����Ϣ
						SUBCMD_S_Gang cmd = new SUBCMD_S_Gang();
						cmd.byActionUser = bySeatID;
						cmd.byOutMjUser = m_MajLogic.GetCurrentUser();
						cmd.block = block;
						cmd.gangType = 2;
						cmd.byMj = Gang.byGangMj;
						cmd.byBuMj = 255;
						cmd.bTing = 0;
						Constants.memset(cmd.bHu, 0, Constants.sizeof(cmd.bHu));

						m_MajLogic.SetCurrentUser(bySeatID);
						for (int j = 0; j < PLAYERCOUNT; j++) {
							Constants.memset(cmd.byHandMj, Constants.INVALID_CARD, Constants.HAND_MJ_MAX);
							if (j == bySeatID) {
								Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
							} else {
								Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
								for (int k = 0; k < Constants.HAND_MJ_MAX; k++) {
									if (cmd.byHandMj[k] != Constants.INVALID_CARD) {
										cmd.byHandMj[k] = 0;
									}
								}
								m_byRight[j][4] = 0;
								cmd.bHu[j] = m_byRight[j][4];
							}

							String sData = cmd.getJsonData();
							SendData(m_pIUserItem[j].dwUserID, sData);
						}

						CleanTempAction();
						// ����������
						OnUserTouchMj(bySeatID, Constants.ACTION_GANG_BUPAI);
					}
						break;
					case 4: // ����
					{
						CleanTempRight(bySeatID);
						MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
						MJ_BLOCK block;
						block = (MJ_BLOCK) RGCPHelper.AnGang(l_MajSide.Maj, Gang.byGangMj);
						RGCPHelper.AddBlock(l_MajSide.hu, block);
						m_MajLogic.GetMajSide(bySeatID).SortCard(Constants.HAND_MJ_MAX);
						huScore[bySeatID] = huScore[bySeatID] + JiangUtil.getAnGangScore(fanJiang1, Jiang, block.first);
						m_MajLogic.SetCurrentAction(Constants.ACTION_GANG);
						// ��Ϣ����
						SUBCMD_S_Gang cmd = new SUBCMD_S_Gang();
						cmd.block = block;
						cmd.byActionUser = bySeatID;
						cmd.gangType = 4;
						cmd.byMj = block.first;
						cmd.byBuMj = 255;
						cmd.bZiMoGang = 0;
						cmd.bTing = 0;
						Constants.memset(cmd.bHu, 0, Constants.sizeof(cmd.bHu));
						for (int j = 0; j < PLAYERCOUNT; j++) {
							Constants.memset(cmd.byHandMj, Constants.INVALID_CARD, Constants.HAND_MJ_MAX);
							if (j == bySeatID) {
								Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
							} else {
								Constants.memcpy(cmd.byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);
								for (int k = 0; k < Constants.HAND_MJ_MAX; k++) {
									if (cmd.byHandMj[k] != Constants.INVALID_CARD) {
										cmd.byHandMj[k] = 0;
									}
								}
							}

							String sData = cmd.getJsonData();
							SendData(m_pIUserItem[j].dwUserID, sData);
						}

						CleanTempAction();
						// ����������
						OnUserTouchMj(bySeatID, Constants.ACTION_GANG_BUPAI);
					}
						break;
					}
				}
			}
		}
		SendYuPai();
		return true;
	}

	boolean OnUserTing(int bySeatID) {
		if (bySeatID >= 4) {
			return false;
		}
		if (m_bGameStarted == false) {
			return false;
		}
		m_MajLogic.SetCurrentAction(Constants.ACTION_TING);
		SUBCMD_S_Ting cmd = new SUBCMD_S_Ting();
		cmd.byActionUser = bySeatID;

		String sData = cmd.getJsonData();
		SendData(m_pIUserItem[bySeatID].dwUserID, sData);
		return false;
	}

	int AdjustHu(int byHuType[]) {

		if (byHuType[Constants.GANG_BAO] > 0) {
			byHuType[Constants.BAO_TOU] = 0;
		}
		if (byHuType[Constants.QI_DUI_ZI_BAO_TOU] > 0) {
			byHuType[Constants.BAO_TOU] = 0;
		}
		int iFanTotal = 0;
		// ����㷬
		for (int i = 0; i < 50; i++) {
			if (byHuType[i] > 0) {
				iFanTotal += byHuType[i];
			}
		}

		return iFanTotal;
	}

	boolean isDaKunEnd() {
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].lUserScore <= 0) {
					return true;
				}
			}
		}
		return false;
	}

	void OnEventGameEnd(int bySeatID, int cbReason) {
		m_bGameStarted = false;

		if (cbReason != Constants.GAME_END_DISMISS) {
			m_wJieSuanRound++;


			SUBCMD_S_JIESUAN cmd = new SUBCMD_S_JIESUAN();

			cmd.byRound = m_byRound;

			cmd.byZhuang = m_byZhuang;

			cmd.byLianZhuang = m_byLianZhuang;

			cmd.bCengBao = m_bCengBao ? 1 : 0;

			cmd.fanJiang = fanJiang1;

			cmd.maizhuang_score = maiZhuangScore;

			for (int i = 0; i < PLAYERCOUNT; i++) {
				cmd.userJieSuan[i].lRoundScore = m_vecRoundScore[i];

				cmd.userJieSuan[i].lRoundHushu = m_vecRoundHushu[i];

				cmd.userJieSuan[i].strHuType = m_vecHuType[i];

				cmd.userJieSuan[i].lRoundHuDesc = m_vecRoundHuDesc[i];

				
				cmd.userJieSuan[i].resultScore = m_vecResultScore[i];

				if (m_vecZiMo[i] == 1) {
					cmd.userJieSuan[i].zimo_fangpao = 2;

				} else {
					if (m_fangpao[i] == 1) {
						cmd.userJieSuan[i].zimo_fangpao = 1;

					} else {
						cmd.userJieSuan[i].zimo_fangpao = 0;
					}
				}

				MajSide l_MajSide = m_MajLogic.GetMajSide(i);
				int hua_count = 0;
				for (int j = 0; j < 8; j++) {
					if (l_MajSide.Hua[j] != Constants.INVALID_CARD) {
						hua_count++;
					}
				}
				cmd.userJieSuan[i].huaCount = hua_count;

				Constants.memcpy(cmd.userJieSuan[i].byHandMj, l_MajSide.Maj, Constants.HAND_MJ_MAX);

				if (m_bIsHu[i] && cmd.userJieSuan[i].byHandMj[Constants.HAND_MJ_MAX - 1] == Constants.INVALID_CARD) {
					cmd.userJieSuan[i].byHandMj[Constants.HAND_MJ_MAX - 1] = m_MajLogic.GetLastOutMj();
				}
				Constants.memcpy(cmd.userJieSuan[i].hu, l_MajSide.hu, Constants.sizeof(cmd.userJieSuan[i].hu));
			}
			SendData(cmd.getJsonData());
		}
		for (int i = 0; i < PLAYERCOUNT; i++) {
			m_kReport[i].lUid = m_pIUserItem[i].dwUserID;
			m_kReport[i].lResultScore = m_vecResultScore[i];
		}
		if (m_byRound <= 1 && cbReason == GAME_END_DISMISS && m_wJieSuanRound < 1) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0L;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;
			SendData(cmd.getJsonData());
			
						
			DelayRemove();

			QpGameMgr.getInstance().OnDismissRoom(this);
            m_bTableStarted = false;
			m_jiesuan = true;
			
		} else if ((m_byRound >= m_byMaxRound || cbReason == GAME_END_DISMISS)&&m_jiesuan == false) {
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.cbType = 0;
			for (int i = 0; i < PLAYERCOUNT; i++) {
				cmd.userReport[i].lUid = m_kReport[i].lUid;
				cmd.userReport[i].cntZiMo = m_kReport[i].cntZiMo;
				cmd.userReport[i].cntHu = m_kReport[i].cntHu;
				cmd.userReport[i].cntDianPao = m_kReport[i].cntDianPao;
				cmd.userReport[i].cntGang = m_kReport[i].cntGang;
				cmd.userReport[i].lResultScore = m_kReport[i].lResultScore;
				m_vecResultScore[i] = m_kReport[i].lResultScore;
			}
			SendData(cmd.getCPJsonData());
			{
				cmd.cbReason = 4;
				cmd.life_time = System.currentTimeMillis() / 1000;
				for (int i = 0; i < PLAYERCOUNT; i++) {
					//dthall.getInstance().addReportMap(m_pIUserItem[i].dwUserID, cmd);
				}
			}

			// ��һ�ֽ��� �۳�����

			int before_cost[] = new int[5];
			int after_cost[] = new int[5];
			int cost[] = new int[5];

			for (int i = 0; i < 5; i++) {
				before_cost[i] = 0;
				cost[i] = 0;
				after_cost[i] = 0;
			}

			for (int i = 0; i < PLAYERCOUNT; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if (user != null) {
						before_cost[i] = user.getGem(0);
					}
				}
			}

			if (m_byRound >= 1) {
				if ("AAPAY".equals(m_payWay)) {
					int cost_gem = m_byCostGem;

					for (int i = 0; i < PLAYERCOUNT; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
							if (user != null) {
								cost[i] = 0 - cost_gem;
								after_cost[i] = user.getGem(0 - cost_gem);
							}
						}
					}
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String created = df.format(new Date());

					for (int i = 0; i < PLAYERCOUNT; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							try {
								JSONObject root_cost = new JSONObject();
								root_cost.put("command", "save_charge_log");
								root_cost.put("created", created);
								root_cost.put("uid", (int) m_pIUserItem[i].dwUserID);
								root_cost.put("before_cost", (int) before_cost[i]);
								root_cost.put("after_cost", (int) after_cost[i]);
								root_cost.put("cost", (int) cost[i]);
								root_cost.put("reason", "�û�" + m_pIUserItem[0].dwUserID + "����  " + m_byMaxRound + "�� "
										+ m_payWay + " ");
								root_cost.put("room_id", m_dwTableID);
								root_cost.put("room_type", 4);
								GameDB.getInstance().push_task(null, root_cost);
							} catch (Exception e) {
							}
						}
					}
				} else if ("WINPAY".equals(m_payWay)) {
					int winScore = 0;

					for (int i = 0; i < m_vecResultScore.length; i++) {
						if (m_vecResultScore[i] >= winScore) {
							winScore = m_vecResultScore[i];
						}
					}
					int winCount = 0;
					for (int i = 0; i < m_vecResultScore.length; i++) {
						if (m_vecResultScore[i] == winScore) {
							winCount++;
						}
					}
					int cost_gem = m_byCostGem;
					cost_gem = cost_gem % winCount == 0 ? cost_gem / winCount : cost_gem / winCount + 1;


					for (int i = 0; i < PLAYERCOUNT; i++) {
						if (m_pIUserItem[i].dwUserID > 0 && m_vecResultScore[i] == winScore) {
							SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
							if (user != null) {
								cost[i] = 0 - cost_gem;
								after_cost[i] = user.getGem(0 - cost_gem);
							}
						}
					}
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String created = df.format(new Date());
					for (int i = 0; i < PLAYERCOUNT; i++) {
						if (m_pIUserItem[i].dwUserID > 0 && m_vecResultScore[i] == winScore) {
							try {
								JSONObject root_cost = new JSONObject();
								root_cost.put("command", "save_charge_log");
								root_cost.put("created", created);
								root_cost.put("uid", (int) m_pIUserItem[i].dwUserID);
								root_cost.put("before_cost", (int) before_cost[i]);
								root_cost.put("after_cost", (int) after_cost[i]);
								root_cost.put("cost", (int) cost[i]);
								root_cost.put("reason", "长牌" + m_pIUserItem[0].dwUserID + "长牌 " + m_byMaxRound + "�� " + m_payWay);
								root_cost.put("room_id", m_dwTableID);
								root_cost.put("room_type", 4);

								GameDB.getInstance().push_task(null, root_cost);
							} catch (Exception e) {
							}
						}
					}
				} else {
					int cost_gem = m_byCostGem;
				
					SHMUser user = null;
					if (m_Creator > 0) {
						user = dthall.getInstance().getUserInfo(m_Creator);

					} else {
						user = dthall.getInstance().getUserInfo(
								m_pIUserItem[0].dwUserID);
					}
					if (user != null) {
						int user_before_cost = user.getGem(0);
						user.getGem(0 - cost_gem);
						int user_after_cost = user.getGem(0);
						
						try {
							SimpleDateFormat df = new SimpleDateFormat(
									"yyyy-MM-dd HH:mm:ss");
							String created = df.format(new Date());
							JSONObject root_cost = new JSONObject();
							root_cost.put("command", "save_charge_log");
							root_cost.put("created", created);
							root_cost.put("uid", (int) m_pIUserItem[0].dwUserID);
							root_cost.put("before_cost", user_before_cost);
							root_cost.put("after_cost", user_after_cost);
							root_cost.put("cost", cost_gem * -1);
							root_cost.put("reason", "长牌" + m_pIUserItem[0].dwUserID + "长牌 " + m_byMaxRound + "�� " + m_payWay);
							root_cost.put("room_id", m_dwTableID);
							root_cost.put("room_type", 4);

							GameDB.getInstance().push_task(null, root_cost);
						} catch (Exception e) {
						}						
					}

				}
			}

			log_counter++;
			log_counter = log_counter % 100;
			Long t = System.currentTimeMillis() / 1000;
			Long battle_id = (t % 1000000) * 100 + log_counter;

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String created = df.format(new Date());
			{

				JSONObject root = new JSONObject();

				Replay.save(m_dwTableID,root);
	
				root.put("command", "save_battle_log");
				root.put("battle_id", battle_id);
				root.put("cost", m_byCostGem);
				root.put("room_type", GetType());

				root.put("room_id", m_dwTableID);
				root.put("creator", GetCreator());

				root.put("jushu", m_byMaxRound);
				root.put("limitscore", 0);

				if (m_payWay == "FZPAY") {
					root.put("payway", 2);
				} else if (m_payWay == "AAPAY") {
					root.put("payway", 1);
				} else {
					root.put("payway", 0);
				}
				root.put("created", created);
				root.put("uid0", m_pIUserItem[0].dwUserID);
				root.put("nickname0", m_pIUserItem[0].szNickName);
				root.put("score0", m_vecResultScore[0]);
				root.put("cost0", cost[0]);
				root.put("headurl0",m_TouXiang[0]);
				root.put("uid1", m_pIUserItem[1].dwUserID);
				root.put("nickname1", m_pIUserItem[1].szNickName);
				root.put("score1", m_vecResultScore[1]);
				root.put("cost1", cost[1]);
				root.put("headurl1",m_TouXiang[1]);
				root.put("uid2", m_pIUserItem[2].dwUserID);
				root.put("nickname2", m_pIUserItem[2].szNickName);
				root.put("score2", m_vecResultScore[2]);
				root.put("cost2", cost[2]);
				root.put("uid3", 0);
				root.put("headurl2",m_TouXiang[2]);
				root.put("nickname3", "");
				root.put("score3", 0);
				root.put("cost3", 0);
				root.put("uid4", 0);
				root.put("nickname4", "");
				root.put("score4", 0);
				root.put("cost4", 0);
								if(m_clubId != 0){
					//club
					root.put("clubid", m_clubId);
					root.put("wfdesc", m_wanFaStr);
					if(m_payWay == "CLUBPAY"){
						root.put("payway", 10);
					}
					ClubRedis.getInstance().addClubBattleLog(m_clubId,root.toString());
				}
					//GameDB.getInstance().push_task(null, root);
				
			}

			
			for (int i = 0; i < PLAYERCOUNT; i++) {// ����ƾ�����
				m_vecResultScore[i] = 0;
			}
			
				
			DelayRemove();

			QpGameMgr.getInstance().OnDismissRoom(this);
           
            m_bTableStarted = false;
			m_jiesuan = true;
		}
	}

	private long time(Object object) {
		return System.currentTimeMillis();
	}

	int GAME_END_DISMISS = Constants.GAME_END_DISMISS;

	private void SendData(String jsonData) {
		SendData(Constants.INVALID_CHAIR, jsonData);
	}

	static int log_counter = 0;

	// public int m_wDealErrorDelay = 0;

	int INVALID_CARD = Constants.INVALID_CARD;
	int INIT_TOTAL = Constants.INIT_TOTAL;

	static int randRobotIndex = 999;

	public void AddRobot() {
		// TODO Auto-generated method stub
		randRobotIndex++;
		int robot_uid = randRobotIndex % 9999 + 200000;

		ArrayList<Integer> vecEmpty = new ArrayList<>();
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				vecEmpty.add(i);
			}
		}
		if (vecEmpty.size() > 0) {
			int rand = Constants.rand();
			int wChairID = vecEmpty.get(rand % vecEmpty.size());

			int vecMaleFace[] = { 1, 3, 5, 7, 9 };
			int vecFemaleFace[] = { 2, 4, 6, 8, 10 };
			m_pIUserItem[wChairID].dwUserID = robot_uid;
			m_pIUserItem[wChairID].lUserScore = m_bIsDaKun ? m_fdFen : 1000;
			m_pIUserItem[wChairID].wChairID = wChairID;
			m_pIUserItem[wChairID].cbGender = (rand % 2);
			if (m_pIUserItem[wChairID].cbGender == 1) {
				m_pIUserItem[wChairID].dwFaceID = vecMaleFace[rand % 5];
			} else {
				m_pIUserItem[wChairID].dwFaceID = vecFemaleFace[rand % 5];
			}
			m_pIUserItem[wChairID].cbUserStatus = US_NULL;
			m_pIUserItem[wChairID].isAndroidUser = 1;

			m_pIUserItem[wChairID].szNickName = "USER[" + wChairID + "]";

			{
				SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
				// memset(&cmd, 0, sizeof(cmd));

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;
				cmd.m_lUserScore = m_pIUserItem[wChairID].lUserScore;
				cmd.m_iAvatar = m_pIUserItem[wChairID].dwFaceID;
				cmd.szNickName = m_pIUserItem[wChairID].szNickName;
				cmd.szIP = m_pIUserItem[wChairID].szIP;

				SendData(cmd.getJsonData());
			}
			{
				m_pIUserItem[wChairID].cbUserStatus = Constants.US_READY;

				SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;

				SendData(cmd.getJsonData());
			}
			m_wRobotSeconds[wChairID] = (long) 0;
			return;
		}
		return;
	}

	int US_NULL = Constants.US_NULL;

	void Handle_onJoin(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbOnline = US_NULL;
		}

		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		SendData(_Uid, cmd.getJsonData());
	}

	public void Handle_onReady(int _Uid, String[] vecArg) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < PLAYERCOUNT) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntReady = 0;
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (m_pIUserItem[i].cbUserStatus == Constants.US_READY || m_pIUserItem[i].isAndroidUser == 1) {
				cntReady++;
			}
		}
		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = bySeatID;
		cmd.m_dwTableID = m_dwTableID;
		SendData(cmd.getJsonData());
		if (cntReady == PLAYERCOUNT) {
			Init(Constants.INIT_NEWROUND);
			m_MajPaiQiang.Ruffle(1500, is_xier);
			for (int i = 0; i < PLAYERCOUNT; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				}
			}
			m_bTableStarted = true;
			m_bGameStarted = true;
			m_byRound = (m_byRound + 1);
			OnRoundInfoChange();
			FaPai_1();
		}
	}
	public void Handle_onMaiZhuang(int _Uid, String[] vecArg) {
		if (maiZhuang == 0) {
			return;
		}
		int is_maizhuang = atoi(vecArg[1]);
		int bySeatID = FindSeat(_Uid);
		if (is_maizhuang != -1 && is_maizhuang != 1) {
			return;
		}

		if (maiZhuangScore[bySeatID] != 0) {
			return;
		}
		maiZhuangScore[bySeatID] = is_maizhuang;

		SUBCMD_S_MAIZHUANG_Score cmd = new SUBCMD_S_MAIZHUANG_Score();
		cmd.maizhuang_score = maiZhuangScore;
		SendData(cmd.getJsonData());

		int index = 0;
		for (int i = 0; i < 3; i++) {
			if (maiZhuangScore[i] == 0) {
				index++;
			}
		}
		if (index == 0) {
			FaPai_2();
		}
	}

	public void Handle_onRobot(int _Uid, String[] vecArg) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < PLAYERCOUNT) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntPlayer = 0;
		for (int i = 0; i < PLAYERCOUNT; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		if (cntPlayer < 4) {
			for (int i = 0; i < PLAYERCOUNT - cntPlayer; i++) {
				AddRobot();
			}
		}
	}

	void Handle_onBuHua(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID != m_MajLogic.GetCurrentUser()) {
			return;
		}
		OnUserBuHua(bySeatID);
	}

	/**
	 * ����
	 * 
	 * @param _Uid
	 * @param vecArg
	 */
	public void Handle_onMoPai(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID != m_MajLogic.GetCurrentUser()) {
			return;
		}
		OnUserTouchMj(bySeatID);
	}

	/**
	 * ����
	 * 
	 * @param _Uid
	 * @param vecArg
	 */
	public void Handle_onFanpai(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);

		if (bySeatID < PLAYERCOUNT) {
			MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
			int byHandMjCount = l_MajSide.GetHandMjCount();
			if (byHandMjCount % 3 == 0) {
				for (int j = Constants.HAND_MJ_MAX - 1; j >= 0; j--) {
					if (l_MajSide.Maj[j] != INVALID_CARD) {
						l_MajSide.Maj[j] = INVALID_CARD;
						break;
					}
				}
			}

			fanJiang1 = m_MajPaiQiang.GetPai();

			SUBCMD_S_FanPai cmd = new SUBCMD_S_FanPai();
			cmd.fanPai1 = fanJiang1;

			// ���ͷ��Ƶ�����
			SendData(cmd.getJsonData());
		}
	}

	// ����: chupai|bySeatID|byOutMj|byActionTingPai
	public void Handle_onChuPai(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);

		if (bySeatID < PLAYERCOUNT) {
			MajSide l_MajSide = m_MajLogic.GetMajSide(bySeatID);
			int byHandMjCount = l_MajSide.GetHandMjCount();
			if (byHandMjCount % 3 == 0) {
				for (int j = Constants.HAND_MJ_MAX - 1; j >= 0; j--) {
					if (l_MajSide.Maj[j] != INVALID_CARD) {
						l_MajSide.Maj[j] = INVALID_CARD;
						break;
					}
				}
			}
			byHandMjCount = l_MajSide.GetHandMjCount();
			if (byHandMjCount % 3 == 2) {
				m_MajLogic.SetCurrentUser(bySeatID);
				int byOutMj = atoi(vecArg[2]);
				OnUserOutMj(bySeatID, byOutMj);
			}
		}
	}

	// ��: pengpai|bySeatID|byPengMj|byOutMj
	public void Handle_onLong(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == INVALID_CHAIR) {
			return;
		}
		SUBCMD_C_Operation peng = new SUBCMD_C_Operation();
		OnUserPeng(bySeatID, peng);
		return;

	}

	// ����: pengpai|bySeatID|byPengMj|byOutMj
	public void Handle_onPengPai(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == INVALID_CHAIR) {
			return;
		}
		SUBCMD_C_Operation peng = new SUBCMD_C_Operation();
		OnUserPeng(bySeatID, peng);
		return;

	}

	// ����: gangpai|bySeatID|byGangMj|byGangMode|byBuMj|byAddBaoPai
	public void Handle_onGangPai(int _Uid, String[] vecArg) {

		int bySeatID = FindSeat(_Uid);
		if (bySeatID == INVALID_CHAIR) {
			return;
		}

		int byGangMj = atoi(vecArg[1]);
		int byGangMode = atoi(vecArg[2]);

		SUBCMD_C_Gang gang = new SUBCMD_C_Gang();
		gang.nRight = 0;
		gang.nGangMode = byGangMode;
		gang.byGangMj = byGangMj;

		OnUserGang(bySeatID, gang);

		return;

	}

	public void Handle_onFangzi_guo(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == INVALID_CHAIR) {
			return;
		}
		if (bySeatID >= PLAYERCOUNT) {
			return;
		}
		if (m_bGameStarted == false) {
			return;
		}
		// Ȩ��У�� �Ƿ��з���Ȩ��
		if (fangzi[bySeatID] == 0) {
			return;
		}
		fangzi[bySeatID] = 0;
		first_buhua(bySeatID, 100);
	}

	// ����: hupai|bySeatID|hu_mode
	public void Handle_onHuPai(int _Uid, String[] vecArg) {

		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			return;
		}

		SUBCMD_C_Hu hu = new SUBCMD_C_Hu();

		OnUserHu(bySeatID, hu);

		return;

	}

	// ����: hupai|bySeatID|hu_mode
	public void Handle_onGuoPai(int _Uid, String[] vecArg) {

		int bySeatID = FindSeat(_Uid);
		if (bySeatID == INVALID_CHAIR) {
			return;
		}

		SUBCMD_C_Operation guo = new SUBCMD_C_Operation();

		OnUserGiveUp(bySeatID, guo);

		return;

	}

	int INVALID_CHAIR = Constants.INVALID_CHAIR;
	  void Handle_onLiWu(int _Uid, String vecArg[]) {
			
			int wChairID = FindSeat(_Uid);
			int index =Integer.parseInt(vecArg[1]);
			int toUser = Integer.parseInt(vecArg[2]);
			int seat = FindSeat(toUser);
			
			if (wChairID == Constants.INVALID_CHAIR||seat == Constants.INVALID_CHAIR) {
				return;
			}
			
			
			SUBCMD_S_LiWu cmd = new SUBCMD_S_LiWu();
			cmd.m_fromSeat = wChairID;
			cmd.m_toSeat = seat;
			cmd.m_index = index;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		}
	// ����
	public void Handle_onBiaoQing(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}

		int index = atoi(vecArg[1]);

		SUBCMD_S_BiaoQing cmd = new SUBCMD_S_BiaoQing();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iBiaoQingIndex = index;

		SendData(cmd.getJsonData());
	}

	// ����
	public void Handle_onYuYin(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == INVALID_CHAIR) {

			return;
		}

		int index = atoi(vecArg[1]);

		SUBCMD_S_YuYin cmd = new SUBCMD_S_YuYin();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iYuYinIndex = index;

		SendData(cmd.getJsonData());
	}

	// ����
	public void Handle_onText(int _Uid, String[] vecArg) {

		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		String strContent = vecArg[1];

		strContent = string_replace(strContent, "|", ".");
		strContent = string_replace(strContent, "#", ".");
		strContent = string_replace(strContent, ",", ".");
		strContent = string_replace(strContent, "\\", ".");
		strContent = string_replace(strContent, "'", ".");
		strContent = string_replace(strContent, "\"", ".");

		SUBCMD_S_Text cmd = new SUBCMD_S_Text();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;
		SendData(cmd.getJsonData());
	}

	private String string_replace(String strContent, String string, String string2) {
		return strContent.replace(string, string2);
	}

	// ����
	void Handle_onSongLi(int _Uid, String[] vecArg) {
	}

	// ����
	public void Handle_onChat(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {

			return;
		}
		String strContent = vecArg[1];

		SUBCMD_S_Chat cmd = new SUBCMD_S_Chat();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;
		SendData(cmd.getJsonData());
	}

	// ����
	public void Handle_onVoice(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int seconds = atoi(vecArg[1]);
		String strVoiceURL = vecArg[2];

		SUBCMD_S_Voice cmd = new SUBCMD_S_Voice();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		cmd.szVoiceURL = strVoiceURL;

		SendData(cmd.getJsonData());
	}

	private int atoi(String string) {
		try {
			return Integer.parseInt(string);
		} catch (Exception e) {
		}
		return 0;
	}

	/**
	 * �����ɢ����
	 * 
	 * @param _Uid
	 * @param vecArg
	 */
	public void Handle_onTryDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 2)
			return;

		String attachInfo = vecArg[1];
		if(attachInfo.equals("fzquit")){
			//房主请求直接退出
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			return;
		}
		int cntUser = 0;
		int cntOnline = 0;
		int cntOffline = 0;
		for (int i = 0; i < 3; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		for (int i = 0; i < 3; i++)
		{
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0)
			{
				if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE)
				{
					cntOffline++;
				}
				else{
					cntOnline++;
				}
			}
		}		
		if (cntUser > 1) {
			if (m_dismissTimer == 0) {
				m_dismissTimer = Constants.time(null);
			}
			m_byAgreeDismiss[bySeatID] = 1;

			SUBCMD_S_TryDismissRoom cmd = new SUBCMD_S_TryDismissRoom();
			cmd.lUid = _Uid;
			cmd.dwTableID = m_dwTableID;
			cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			
			if (cntOnline == 1)
			{
				OnEventGameEnd(0,Constants.GAME_END_DISMISS);
				DelayRemove();
				QpGameMgr.getInstance().OnDismissRoom(this);

				m_dismissTimer = 0;
			}			
		} else {
			OnEventGameEnd(0,Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
		}
	}
public void OnTryDismissRoom(int _Uid, String vecArg[]){		
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			return;
		}
		if (vecArg.length < 2)
			return;
	
		String attachInfo = vecArg[1];
		if(attachInfo.equals("fzquit")){
			//房主请求直接退出
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			return;
		}
		int cntUser = 0;
		int cntOnline = 0;
		int cntOffline = 0;
		for (int i = 0; i < 3; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		for (int i = 0; i < 3; i++)
		{
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0)
			{
				if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE)
				{
					cntOffline++;
				}
				else{
					cntOnline++;
				}
			}
		}		
		if (cntUser > 1) {
			if (m_dismissTimer == 0) {
				m_dismissTimer = Constants.time(null);
			}
			m_byAgreeDismiss[bySeatID] = 1;
	
			SUBCMD_S_TryDismissRoom cmd = new SUBCMD_S_TryDismissRoom();
			cmd.lUid = _Uid;
			cmd.dwTableID = m_dwTableID;
			cmd.szNickName = m_pIUserItem[bySeatID].szNickName;
	
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			
			if (cntOnline == 1)
			{
				OnEventGameEnd(0,Constants.GAME_END_DISMISS);
				DelayRemove();
				QpGameMgr.getInstance().OnDismissRoom(this);
	
				m_dismissTimer = 0;
			}			
		} else {
			OnEventGameEnd(0,Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
		}
	}
	/**
	 * ͬ���ɢ����
	 * 
	 * @param _Uid
	 * @param vecArg
	 */
	public void Handle_onAgreeDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		m_byAgreeDismiss[bySeatID] = 1;

		SUBCMD_S_AgreeDismissRoom cmd = new SUBCMD_S_AgreeDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		if (bySeatID == 0 && m_bTableStarted == false) {
			OnEventGameEnd(0,Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			return;
		}
		int cntAgree = 0;
		for (int i = 0; i < 3; i++) {
			if (m_byAgreeDismiss[i] > 0) {
				cntAgree++;
			}
		}
		int cntUser = 0;
		for (int i = 0; i < 3; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		if (cntAgree >= 2 && cntAgree >= cntUser - 1) {
			OnEventGameEnd(0,Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

			if (ShouldBeRemoved()) {
				// niuniuTableMgr::instance().DismissTable(this);
			}
		}
	}

	/**
	 * �ܾ���ɢ����
	 * 
	 * @param _Uid
	 * @param vecArg
	 */
	public void Handle_onRefuseDismissRoom(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;

		m_dismissTimer = 0;
		m_byAgreeDismiss[0] = 0;
		m_byAgreeDismiss[1] = 0;
		m_byAgreeDismiss[2] = 0;
	
		SUBCMD_S_RefuseDismissRoom cmd = new SUBCMD_S_RefuseDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	public int m_DealTime = 0;

	@Override
	public int getDealTime() {
		return m_DealTime;
	}

	public boolean m_bRemove = false;

	public void DelayRemove() {
		m_bRemove = true;
	}

	public void ResetKillFlag() {
		m_bRemove = false;
	}

	public boolean ShouldBeRemoved() {
		return m_bRemove;
	}

	@Override
	public String getRoomInfo() {
	
		String roomInfo = String.format("%s--%s--%s", 
				m_pIUserItem[0].GetUserInfo(), //#
				m_pIUserItem[1].GetUserInfo(), 
				m_pIUserItem[2].GetUserInfo()
				);
		
		String[] vecParam = m_strParam.split(";");

		int jiesuan = m_jiesuan? 1:0;
		
		roomInfo = String.format("%s|%d|%d|%d|%d|%d|%s|%s|%d", roomInfo,m_dwTableID,maxPeople,m_byRound,m_byMaxRound,jiesuan,"���Ƶ���","CP",m_Creator);
		
		return roomInfo;
	}

	@Override
	public int getPlayerCount() {
		int cntUser = 0;
		for (int i = 0; i < 3; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		return cntUser;
	}

	@Override
	public int getCostGem() {
		return m_byCostGem;
	}

	@Override
	public int getStatus() {
		if (m_bTableStarted == false) {
			return 0;
		}
		return 1;
	}

	@Override
	public int getCellScore() {
		return 0;
	}

	@Override
	public int getMinScore() {
		return 0;
	}
	void OnDealAI(int dt) {
		m_DealTime = (int) (System.currentTimeMillis() / 1000);
	}
	@Override
	public void OnDeal(int dt) {
		if (m_bGameStarted == true) {
			if (GetRobotCount() == GetPlayerCount()) {
				Init(INIT_TOTAL);
			} else {
				OnDealAI(dt);
			}
		}
		if (m_bGameStarted == false) {
		}
	}

	@Override
	public String GetRoundInfo() {
		return "" + m_byRound + "/" +  m_byMaxRound;
	}

	@Override
	public String getRoomDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPayway() {
		return m_payWay;
	}
}

class SUBCMD_C_Operation {
	int byCurrentUser; // ��ǰ�û�
	int byLastUser; // �ϴγ����û�
	int nRight; // ����,6-��,4-��,3-��,2-��, 0-����
};

// �û�����
class SUBCMD_C_OutMj {
	int byOutMj; // �������
};

// �Բ���
class SUBCMD_C_Chi {
	int nRight; // Ȩ��
	int nChiType; // ��������,0-��,1-@@*, 2-@*@, 3-*@@
};

// ������������������Բ�������

// �ܲ���
class SUBCMD_C_Gang {
	int nRight; // Ȩ��
	int nGangMode; // ��������,0-��, 1-����,2-����, 4-����
	int byGangMj; // ���ܵ��ƣ����������ж��ָ��ƵĿ���
};

// ����ѡ��
class SUBCMD_C_SelectGang {
	int nGangType; // ��������
	int byGangMj; // ���ܵ���
};

// ������
class SUBCMD_C_Hu {
	int nRight; // ����Ȩ��
};

class M_tempAction {
	int m_ActionRight;
	// �û�����
	SUBCMD_C_Operation m_cOperation = new SUBCMD_C_Operation();
	SUBCMD_C_OutMj m_cChuPai = new SUBCMD_C_OutMj();
	// �Բ���
	SUBCMD_C_Chi m_cChi = new SUBCMD_C_Chi();
	// ������������������Բ�������
	SUBCMD_C_Operation m_cPeng = new SUBCMD_C_Operation();
	// �ܲ���
	SUBCMD_C_Gang m_cGang = new SUBCMD_C_Gang();
	SUBCMD_C_SelectGang m_cSelectGang = new SUBCMD_C_SelectGang();
	// ������
	SUBCMD_C_Hu m_cHu = new SUBCMD_C_Hu();
	// �ܲ���
	SUBCMD_C_Operation m_cGuo = new SUBCMD_C_Operation();

	void Clean() {

		m_ActionRight = 0;
	}
};

class SUBACTION_C_FaPai {
	int byRound;
	int byZhuang;
	int byLianZhuang;
	int byMaxRound;
	int byHandMj[][] = new int[4][Constants.HAND_MJ_MAX];
	int byGuiPai[] = new int[2];
	int lazhuang_score[] = new int[4];
	String room_info;
	int room_id;
	int fanJiang;
	int[] maizhuang_score;

	public String toString() {
		String strHandMj0 = IJsonExport.sprintf_BYTE(byHandMj[0], Constants.HAND_MJ_MAX);
		String strHandMj1 = IJsonExport.sprintf_BYTE(byHandMj[1], Constants.HAND_MJ_MAX);
		String strHandMj2 = IJsonExport.sprintf_BYTE(byHandMj[2], Constants.HAND_MJ_MAX);
		String strHandMj3 = IJsonExport.sprintf_BYTE(byHandMj[3], Constants.HAND_MJ_MAX);
		String maizhuang_scoreStr = IJsonExport.sprintf_BYTE(maizhuang_score, 3);

		return String.format("%d#%d#%d#%d#%s#%s#%s#%s#%d,%d#%d,%d,%d,%d#%s#%d#%d#%s", byRound, byZhuang, byLianZhuang,
				byMaxRound, strHandMj0, strHandMj1, strHandMj2, strHandMj3, byGuiPai[0], byGuiPai[1], lazhuang_score[0],
				lazhuang_score[1], lazhuang_score[2], lazhuang_score[3], room_info, room_id, fanJiang,
				maizhuang_scoreStr);
	}
};

class SUBACTION_C_BuHua {
	int bySeatID;
	int hua;

	public String toString() {
		return String.format("%d#%d", bySeatID, hua);
	}
}

// �û�����
class SUBACTION_C_MoPai {
	int bySeatID;
	int byMj; // �������

	public String toString() {
		return String.format("%d#%d", bySeatID, byMj);
	}
};

// �û�����
class SUBACTION_C_ChuPai {
	int bySeatID;
	int byMj; // �������
	int nTing;

	public String toString() {
		return String.format("%d#%d#%d", bySeatID, byMj, nTing);
	}
};

// �Բ���
class SUBACTION_C_ChiPai {
	int bySeatID;
	int byMj;
	BLOCK block = new BLOCK(); // ���ƿ�
	int nChiType; // ��������,0-��,1-@@*, 2-@*@, 3-*@@

	public String toString() {
		return String.format("%d#%d#%d,%d#%d", bySeatID, byMj, block.style, block.first, nChiType);
	}
};

class SUBACTION_C_long {
	int byRound;
	int byZhuang;
	int byLianZhuang;
	int byMaxRound;
	int byHandMj[][] = new int[4][Constants.HAND_MJ_MAX];
	int byGuiPai[] = new int[2];
	int lazhuang_score[] = new int[4];
	String room_info;

	public String toString() {
		String strHandMj0 = IJsonExport.sprintf_BYTE(byHandMj[0], Constants.HAND_MJ_MAX);
		String strHandMj1 = IJsonExport.sprintf_BYTE(byHandMj[1], Constants.HAND_MJ_MAX);
		String strHandMj2 = IJsonExport.sprintf_BYTE(byHandMj[2], Constants.HAND_MJ_MAX);
		String strHandMj3 = IJsonExport.sprintf_BYTE(byHandMj[3], Constants.HAND_MJ_MAX);

		return String.format("%d#%d#%d#%d#%s#%s#%s#%s#%d,%d#%d,%d,%d,%d#%s", byRound, byZhuang, byLianZhuang,
				byMaxRound, strHandMj0, strHandMj1, strHandMj2, strHandMj3, byGuiPai[0], byGuiPai[1], lazhuang_score[0],
				lazhuang_score[1], lazhuang_score[2], lazhuang_score[3], room_info);
	}
};

// ������������������Բ�������
// �Բ���
class SUBACTION_C_Peng {
	int bySeatID;
	int byMj;
	BLOCK block = new BLOCK(); // ���ƿ�

	public String toString() {
		return String.format("%d#%d#%d,%d", bySeatID, byMj, block.style, block.first);
	}
};

// �ܲ���
class SUBACTION_C_FangziGang {
	int bySeatID;
	int byMj;
	BLOCK block = new BLOCK(); // ���ƿ�
	int nGangMode; // ��������,0-��, 1-����,2-����, 4-����
	int byGangMj; // ���ܵ��ƣ����������ж��ָ��ƵĿ���

	public String toString() {
		return String.format("%d#%d#%d,%d#%d#%d#%d", bySeatID, byMj, block.style, block.first, nGangMode, byGangMj,
				block.count);
	}
};

class SUBACTION_C_FirstBuHua {
	int bySeatID;
	int byHandMj[] = new int[Constants.HAND_MJ_MAX];
	int hua[] = new int[8];

	public String toString() {
		String handMjStr = IJsonExport.sprintf_INT32(byHandMj, byHandMj.length);
		String huaStr = IJsonExport.sprintf_INT32(hua, hua.length);
		return String.format("%d#%s#%s", bySeatID, handMjStr, huaStr);
	}
}

// �ܲ���
class SUBACTION_C_Gang {
	int bySeatID;
	int byMj;
	BLOCK block = new BLOCK(); // ���ƿ�
	int nGangMode; // ��������,0-��, 1-����,2-����, 4-����
	int byGangMj; // ���ܵ��ƣ����������ж��ָ��ƵĿ���

	public String toString() {
		return String.format("%d#%d#%d,%d#%d#%d", bySeatID, byMj, block.style, block.first, nGangMode, byGangMj);
	}
};

// ������
class SUBACTION_C_Hu {
	int byRound; //
	int byZhuang; //
	int byLianZhuang;
	tagJieSuan userJieSuan[] = new tagJieSuan[4];
	int bCengBao;
	int vecMaPai[] = new int[6];
	int[] guipai = new int[2];
	int fanJiang;
	int[] maizhuang_score;

	{
		for (int i = 0; i < userJieSuan.length; i++) {
			userJieSuan[i] = new tagJieSuan();
		}
	}

	public String toString() {
		String strJieSuan0 = userJieSuan[0].toString();
		String strJieSuan1 = userJieSuan[1].toString();
		String strJieSuan2 = userJieSuan[2].toString();
		String strJieSuan3 = userJieSuan[3].toString();

		String strMaPai = IJsonExport.sprintf_BYTE(vecMaPai, 6);
		String maizhuang_scoreStr = IJsonExport.sprintf_BYTE(maizhuang_score, 3);
		String strGuiPai = "";
		String cpmsg = String.format("jiesuan|%d|%d|%d|%s|%s|%s|%s|%d|%s|%s|%d|%s", byRound, byZhuang, byLianZhuang,
				strJieSuan0, strJieSuan1, strJieSuan2, strJieSuan3, bCengBao, strMaPai, strGuiPai, fanJiang,
				maizhuang_scoreStr);

		return cpmsg;
	}
};
